473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Runtime error 91 ??

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.Application
Dim objEmail As Outlook.MailItem
Dim strLtrContent As String
Dim ExcelApp As New Excel.Application
Dim ExcelFile As New Excel.Workbook
Dim ExcelSheet As New Excel.Worksheet
Dim SheetName As String
Dim rsSmartData As New ADODB.Recordset

rsSmartData.ActiveConnection = CurrentProject.Connection
rsSmartData.Open "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("Customer 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 & "Distributed Provisioning"

' This section is designed to open up the standard customer questionnaire
' and save the details per line item within this loop

ExcelApp.Visible = True
Set ExcelFile = ExcelApp.Workbooks.Open("S:\Admin- BF\ESCQ\Questionaire\ECSQ.xls")
Set ExcelSheet = ExcelApp.Worksheets("Answers")

With ExcelSheet

ActiveSheet.Unprotect ("****")

.Range("C2").Value = rsSmartData("Job ref")
.Range("D2").Value = rsSmartData("Unique Job Ref")
.Range("F2").Value = rsSmartData("Catalogue Description")
.Range("G2").Value = rsSmartData("Product Name")

ActiveSheet.Protect ("****")

End With

Excel.Application.DisplayAlerts = 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.CreateItem(olMailItem)
objEmail.Recipients.Add rsSmartData("Recipient ID")
objEmail.Subject = "Order IT Customer Satisfaction Survey. "
objEmail.Body = strLtrContent
objEmail.Attachments.Add ("S:\Admin- BF\ESCQ\Questionaire\ECSQ.xls")

objEmail.Send

rsSmartData.MoveNext

Loop

End Sub
Nov 21 '07 #1
6 4262
ADezii
8,834 Expert 8TB
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.Application
Dim objEmail As Outlook.MailItem
Dim strLtrContent As String
Dim ExcelApp As New Excel.Application
Dim ExcelFile As New Excel.Workbook
Dim ExcelSheet As New Excel.Worksheet
Dim SheetName As String
Dim rsSmartData As New ADODB.Recordset

rsSmartData.ActiveConnection = CurrentProject.Connection
rsSmartData.Open "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("Customer 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 & "Distributed Provisioning"

' This section is designed to open up the standard customer questionnaire
' and save the details per line item within this loop

ExcelApp.Visible = True
Set ExcelFile = ExcelApp.Workbooks.Open("S:\Admin- BF\ESCQ\Questionaire\ECSQ.xls")
Set ExcelSheet = ExcelApp.Worksheets("Answers")

With ExcelSheet

ActiveSheet.Unprotect ("****")

.Range("C2").Value = rsSmartData("Job ref")
.Range("D2").Value = rsSmartData("Unique Job Ref")
.Range("F2").Value = rsSmartData("Catalogue Description")
.Range("G2").Value = rsSmartData("Product Name")

ActiveSheet.Protect ("****")

End With

Excel.Application.DisplayAlerts = 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.CreateItem(olMailItem)
objEmail.Recipients.Add rsSmartData("Recipient ID")
objEmail.Subject = "Order IT Customer Satisfaction Survey. "
objEmail.Body = strLtrContent
objEmail.Attachments.Add ("S:\Admin- BF\ESCQ\Questionaire\ECSQ.xls")

objEmail.Send

rsSmartData.MoveNext

Loop

End Sub
The problem seems to be that your are Protecting/Unprotecting the Worksheet within the context of the With...End With Construct:
Expand|Select|Wrap|Line Numbers
  1. With ExcelSheet
  2.   ActiveSheet.Unprotect ("****")
  3.  
  4.   .Range("C2").Value = rsSmartData("Job ref")
  5.   .Range("D2").Value = rsSmartData("Unique Job Ref")
  6.   .Range("F2").Value = rsSmartData("Catalogue Description")
  7.   .Range("G2").Value = rsSmartData("Product Name")
  8.  
  9.   ActiveSheet.Protect ("****")
  10. End With
Which may evaluate to:
Expand|Select|Wrap|Line Numbers
  1. ExcelSheetActiveSheet.Unprotect ("****")
  2. ExcelSheetActiveSheet.Protect ("****")
which is of course, meaningless.
Nov 21 '07 #2
Thanks i've tried moving the code as below but obtain the same error, if I unprotect the file being opened and disable the unprotect / protect code within this module the script runs as required ??? Unfortunately I really need the protection on this document, any thoughts?

Expand|Select|Wrap|Line Numbers
  1. Activesheet.Unprotect("****")
  2.  
  3. With ExcelSheet
  4.  
  5.   .Range("C2").Value = rsSmartData("Job ref")
  6.   .Range("D2").Value = rsSmartData("Unique Job Ref")
  7.   .Range("F2").Value = rsSmartData("Catalogue Description")
  8.   .Range("G2").Value = rsSmartData("Product Name")
  9.  
  10. End With
  11.  
  12. Activesheet.Protect("****")
Nov 22 '07 #3
FishVal
2,653 Expert 2GB
Hi, Smigger.

The code is executed in Access module. Am I right? If so, then you should refer to Excel.Application explicitely.
Expand|Select|Wrap|Line Numbers
  1. ExcelApp.Activesheet.Unprotect("****")
  2.  
or even better if you have object variable referenced to target Worksheet, then it is more reliable to run Unprotect method of it.
Expand|Select|Wrap|Line Numbers
  1. With ExcelSheet
  2.   .Activesheet.Unprotect("****")
  3.   .Range("C2").Value = rsSmartData("Job ref")
  4.   .Range("D2").Value = rsSmartData("Unique Job Ref")
  5.   .Range("F2").Value = rsSmartData("Catalogue Description")
  6.   .Range("G2").Value = rsSmartData("Product Name")
  7.   .Activesheet.Protect("****")
  8. End With
  9.  
Regards,
Fish.
Nov 22 '07 #4
ADezii
8,834 Expert 8TB
Thanks i've tried moving the code as below but obtain the same error, if I unprotect the file being opened and disable the unprotect / protect code within this module the script runs as required ??? Unfortunately I really need the protection on this document, any thoughts?

Expand|Select|Wrap|Line Numbers
  1. Activesheet.Unprotect("****")
  2.  
  3. With ExcelSheet
  4.  
  5.   .Range("C2").Value = rsSmartData("Job ref")
  6.   .Range("D2").Value = rsSmartData("Unique Job Ref")
  7.   .Range("F2").Value = rsSmartData("Catalogue Description")
  8.   .Range("G2").Value = rsSmartData("Product Name")
  9.  
  10. End With
  11.  
  12. Activesheet.Protect("****")
I think FishVal gave you some excellant advice. I tested the following subset of your code and it works flawlessly. Just parallel this code, and you shouold be OK.
Expand|Select|Wrap|Line Numbers
  1. Dim ExcelApp As New Excel.Application
  2. Dim ExcelFile As New Excel.Workbook
  3. Dim ExcelSheet As New Excel.Worksheet
  4. Dim SheetName As String
  5.  
  6.  
  7. ExcelApp.Visible = True
  8. Set ExcelFile = ExcelApp.Workbooks.Open("C:\Test\Test.xls")
  9. Set ExcelSheet = ExcelApp.Worksheets("Sheet1")
  10.  
  11. ExcelSheet.Unprotect Password:="TheProtector"
  12.  
  13. With ExcelSheet
  14.   .Range("C2").Value = "Job ref"
  15.   .Range("D2").Value = "Unique Job Ref"
  16.   .Range("F2").Value = "Catalogue Description"
  17.   .Range("G2").Value = "Product Name"
  18. End With
  19.  
  20. ExcelSheet.Protect Password:="TheProtector"
  21.  
  22. Excel.Application.DisplayAlerts = False
  23. ExcelFile.Save
  24. ExcelFile.Close True
  25. ExcelApp.Quit
  26.  
  27. Set ExcelSheet = Nothing
  28. Set ExcelFile = Nothing
  29. Set ExcelApp = Nothing
  30.  
Nov 22 '07 #5
Yes you absolutely right this is being run in an Access Module.

I'd already previously tried the code indicated within your response and shown below; however, I am still getting the same runtime error message.

Expand|Select|Wrap|Line Numbers
  1. Excel.Application.Activesheet.Unprotect("password")
  2.  
  3. With ExcelSheet
  4.  
  5.   .Range("C2").Value = rsSmartData("Job ref")
  6.   .Range("D2").Value = rsSmartData("Unique Job Ref")
  7.   .Range("F2").Value = rsSmartData("Catalogue Description")
  8.   .Range("G2").Value = rsSmartData("Product Name")
  9.  
  10. End With
  11.  
  12. Excel.Application.Activesheet.Protect("password")
Below is the sum of the code within the access module I'm using at the moment, as mentioned before, if the protection lines 31 and 42 are removed everthing works as expected......

Expand|Select|Wrap|Line Numbers
  1. Sub ControlOutlook()
  2.  
  3. Dim objOutlook As New Outlook.Application
  4. Dim objEmail As Outlook.MailItem
  5. Dim strLtrContent As String
  6. Dim ExcelApp As New Excel.Application
  7. Dim ExcelFile As New Excel.Workbook
  8. Dim ExcelSheet As New Excel.Worksheet
  9. Dim rsSmartData As New ADODB.Recordset
  10.  
  11. rsSmartData.ActiveConnection = CurrentProject.Connection
  12. rsSmartData.Open "SmartData"
  13.  
  14. 'For each record in the Smart Data table send and e-mail
  15.  
  16. Do While Not rsSmartData.EOF
  17.  
  18. strLtrContent = "Dear" & " " & rsSmartData("Customer Name") & ":" & vbCrLf & vbCrLf
  19. strLtrContent = strLtrContent & "You have been selected to take part in our customer satisfaction survey, "
  20. strLtrContent = strLtrContent & "Would you please take the time to compelete the attached form. "
  21. strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Please do NOT reply to the sender of this e-mail"
  22. strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Sincerly," & vbCrLf & vbCrLf
  23. strLtrContent = strLtrContent & "Distributed Provisioning"
  24.  
  25. ' Excel call code here
  26.  
  27. ExcelApp.Visible = True
  28. Set ExcelFile = ExcelApp.Workbooks.Open("S:\Admin- BF\ESCQ\Questionaire\ECSQ.xls")
  29. Set ExcelSheet = ExcelApp.Worksheets("Answers")
  30.  
  31. Excel.Application.ActiveSheet.Unprotect ("password")
  32.  
  33.     With ExcelSheet
  34.  
  35.         .Range("C2").Value = rsSmartData("Order ID")
  36.         .Range("D2").Value = rsSmartData("Orderline ID")
  37.         .Range("F2").Value = rsSmartData("Product Description")
  38.         .Range("G2").Value = rsSmartData("Product Name")
  39.  
  40.     End With
  41.  
  42. Excel.Application.ActiveSheet.Protect ("password")
  43.  
  44. Excel.Application.DisplayAlerts = False
  45. ExcelFile.Save
  46. ExcelFile.Close True
  47. ExcelApp.Quit
  48.  
  49. Set ExcelSheet = Nothing
  50. Set ExcelFile = Nothing
  51. Set ExcelApp = Nothing
  52.  
  53. ' Excel call code here
  54.  
  55. Set objEmail = objOutlook.CreateItem(olMailItem)
  56. objEmail.Recipients.Add rsSmartData("Recipient ID")
  57. objEmail.Subject = "Order IT Customer Satisfaction Survey. "
  58. objEmail.Body = strLtrContent
  59. objEmail.Attachments.Add ("S:\Admin- BF\ESCQ\Questionaire\ECSQ.xls")
  60.  
  61. objEmail.Send
  62.  
  63. rsSmartData.MoveNext
  64.  
  65. Loop
  66.  
  67. End Sub
Nov 22 '07 #6
All resolved now, many thanks for your asistances.

The problem was an oversite on my part, I see now that the protection code should have read as below, without the Activesheet scripting

Expand|Select|Wrap|Line Numbers
  1.     With ExcelSheet
  2.  
  3.         .Unprotect ("password")
  4.         .Range("C2").Value = rsSmartData("Order ID")
  5.         .Range("D2").Value = rsSmartData("Orderline ID")
  6.         .Range("F2").Value = rsSmartData("Product Description")
  7.         .Range("G2").Value = rsSmartData("Product Name")
  8.         .Protect ("password")
  9.  
  10.     End With
Nov 22 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Nathan Bloom | last post by:
Hi, I have a secured database that runs fine on the computer the database is installed on. I have several workstations with access runtime installed that also need access to the database. ...
2
by: Paul | last post by:
I'm hoping someone can help me with the problem. I have a database where the default value for a date field in a table is =date() This works perfectly in my table and on my form. But when I...
0
by: Kirk | last post by:
I'm trying to use a Web Service to be a Remoting client of an existing ..NET 2.0 server. But I get the following error when I try to use System.Runtime.Remoting.Channels.Http in my WebService. ...
2
by: Kirk | last post by:
I'm trying to use a Web Service to be a Remoting client of an existing ..NET 2.0 server. But I get the following error when I try to use System.Runtime.Remoting.Channels.Http in my WebService. ...
0
by: zfraile | last post by:
I'm getting this error from the JIT compiler at runtime, but only on my boss' machine, not my development machine. I believe the error is generated from a call to an Excel object, but I can't tell...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.