Afternoon,
I'm having a little trouble with the code listed below, in Access 2003 which is opening and editing an existing Excel 2003 workbook.
The section of code works the first time round, however, on the second pass the Runtime error 91 comes to haunt me at the line highlighted in bold below.
The idea of the script is to update a standard customer questionaire (xls file) with some basic details from a database and then attach the now updated personalized excel file and mails it to the user
Sub ControlOutlook( )
Dim objOutlook As New Outlook.Applica tion
Dim objEmail As Outlook.MailIte m
Dim strLtrContent As String
Dim ExcelApp As New Excel.Applicati on
Dim ExcelFile As New Excel.Workbook
Dim ExcelSheet As New Excel.Worksheet
Dim SheetName As String
Dim rsSmartData As New ADODB.Recordset
rsSmartData.Act iveConnection = CurrentProject. Connection
rsSmartData.Ope n "SmartData"
'For each record in the Smart Data table send and e-mail
Do While Not rsSmartData.EOF
' mail construct goes here
strLtrContent = "Dear" & " " & rsSmartData("Cu stomer Name") & ":" & vbCrLf & vbCrLf
strLtrContent = strLtrContent & "You have been selected to take part in our customer satisfaction survey, "
strLtrContent = strLtrContent & "Would you please take the time to compelete the attached form. "
strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Please do NOT reply to the sender of this e-mail"
strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Sincerly," & vbCrLf & vbCrLf
strLtrContent = strLtrContent & "Distribute d Provisioning"
' This section is designed to open up the standard customer questionnaire
' and save the details per line item within this loop
ExcelApp.Visibl e = True
Set ExcelFile = ExcelApp.Workbo oks.Open("S:\Ad min- BF\ESCQ\Questio naire\ECSQ.xls" )
Set ExcelSheet = ExcelApp.Worksh eets("Answers")
With ExcelSheet
ActiveSheet.Unp rotect ("****")
.Range("C2").Va lue = rsSmartData("Jo b ref")
.Range("D2").Va lue = rsSmartData("Un ique Job Ref")
.Range("F2").Va lue = rsSmartData("Ca talogue Description")
.Range("G2").Va lue = rsSmartData("Pr oduct Name")
ActiveSheet.Pro tect ("****")
End With
Excel.Applicati on.DisplayAlert s = False
ExcelFile.Save
ExcelFile.Close True
ExcelApp.Quit
Set ExcelSheet = Nothing
Set ExcelFile = Nothing
Set ExcelApp = Nothing
' Prepares the mail for sending
Set objEmail = objOutlook.Crea teItem(olMailIt em)
objEmail.Recipi ents.Add rsSmartData("Re cipient ID")
objEmail.Subjec t = "Order IT Customer Satisfaction Survey. "
objEmail.Body = strLtrContent
objEmail.Attach ments.Add ("S:\Admin- BF\ESCQ\Questio naire\ECSQ.xls" )
objEmail.Send
rsSmartData.Mov eNext
Loop
End Sub
I'm having a little trouble with the code listed below, in Access 2003 which is opening and editing an existing Excel 2003 workbook.
The section of code works the first time round, however, on the second pass the Runtime error 91 comes to haunt me at the line highlighted in bold below.
The idea of the script is to update a standard customer questionaire (xls file) with some basic details from a database and then attach the now updated personalized excel file and mails it to the user
Sub ControlOutlook( )
Dim objOutlook As New Outlook.Applica tion
Dim objEmail As Outlook.MailIte m
Dim strLtrContent As String
Dim ExcelApp As New Excel.Applicati on
Dim ExcelFile As New Excel.Workbook
Dim ExcelSheet As New Excel.Worksheet
Dim SheetName As String
Dim rsSmartData As New ADODB.Recordset
rsSmartData.Act iveConnection = CurrentProject. Connection
rsSmartData.Ope n "SmartData"
'For each record in the Smart Data table send and e-mail
Do While Not rsSmartData.EOF
' mail construct goes here
strLtrContent = "Dear" & " " & rsSmartData("Cu stomer Name") & ":" & vbCrLf & vbCrLf
strLtrContent = strLtrContent & "You have been selected to take part in our customer satisfaction survey, "
strLtrContent = strLtrContent & "Would you please take the time to compelete the attached form. "
strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Please do NOT reply to the sender of this e-mail"
strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Sincerly," & vbCrLf & vbCrLf
strLtrContent = strLtrContent & "Distribute d Provisioning"
' This section is designed to open up the standard customer questionnaire
' and save the details per line item within this loop
ExcelApp.Visibl e = True
Set ExcelFile = ExcelApp.Workbo oks.Open("S:\Ad min- BF\ESCQ\Questio naire\ECSQ.xls" )
Set ExcelSheet = ExcelApp.Worksh eets("Answers")
With ExcelSheet
ActiveSheet.Unp rotect ("****")
.Range("C2").Va lue = rsSmartData("Jo b ref")
.Range("D2").Va lue = rsSmartData("Un ique Job Ref")
.Range("F2").Va lue = rsSmartData("Ca talogue Description")
.Range("G2").Va lue = rsSmartData("Pr oduct Name")
ActiveSheet.Pro tect ("****")
End With
Excel.Applicati on.DisplayAlert s = False
ExcelFile.Save
ExcelFile.Close True
ExcelApp.Quit
Set ExcelSheet = Nothing
Set ExcelFile = Nothing
Set ExcelApp = Nothing
' Prepares the mail for sending
Set objEmail = objOutlook.Crea teItem(olMailIt em)
objEmail.Recipi ents.Add rsSmartData("Re cipient ID")
objEmail.Subjec t = "Order IT Customer Satisfaction Survey. "
objEmail.Body = strLtrContent
objEmail.Attach ments.Add ("S:\Admin- BF\ESCQ\Questio naire\ECSQ.xls" )
objEmail.Send
rsSmartData.Mov eNext
Loop
End Sub
Comment