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
6 4262
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: -
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
Which may evaluate to: - ExcelSheetActiveSheet.Unprotect ("****")
-
ExcelSheetActiveSheet.Protect ("****")
which is of course, meaningless.
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? - Activesheet.Unprotect("****")
-
-
With ExcelSheet
-
-
.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")
-
-
End With
-
-
Activesheet.Protect("****")
Hi, Smigger.
The code is executed in Access module. Am I right? If so, then you should refer to Excel.Application explicitely. -
ExcelApp.Activesheet.Unprotect("****")
-
or even better if you have object variable referenced to target Worksheet, then it is more reliable to run Unprotect method of it. -
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
-
Regards,
Fish.
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? - Activesheet.Unprotect("****")
-
-
With ExcelSheet
-
-
.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")
-
-
End With
-
-
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. -
Dim ExcelApp As New Excel.Application
-
Dim ExcelFile As New Excel.Workbook
-
Dim ExcelSheet As New Excel.Worksheet
-
Dim SheetName As String
-
-
-
ExcelApp.Visible = True
-
Set ExcelFile = ExcelApp.Workbooks.Open("C:\Test\Test.xls")
-
Set ExcelSheet = ExcelApp.Worksheets("Sheet1")
-
-
ExcelSheet.Unprotect Password:="TheProtector"
-
-
With ExcelSheet
-
.Range("C2").Value = "Job ref"
-
.Range("D2").Value = "Unique Job Ref"
-
.Range("F2").Value = "Catalogue Description"
-
.Range("G2").Value = "Product Name"
-
End With
-
-
ExcelSheet.Protect Password:="TheProtector"
-
-
Excel.Application.DisplayAlerts = False
-
ExcelFile.Save
-
ExcelFile.Close True
-
ExcelApp.Quit
-
-
Set ExcelSheet = Nothing
-
Set ExcelFile = Nothing
-
Set ExcelApp = Nothing
-
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. - Excel.Application.Activesheet.Unprotect("password")
-
-
With ExcelSheet
-
-
.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")
-
-
End With
-
-
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...... - 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 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
-
-
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"
-
-
' Excel call code here
-
-
ExcelApp.Visible = True
-
Set ExcelFile = ExcelApp.Workbooks.Open("S:\Admin- BF\ESCQ\Questionaire\ECSQ.xls")
-
Set ExcelSheet = ExcelApp.Worksheets("Answers")
-
-
Excel.Application.ActiveSheet.Unprotect ("password")
-
-
With ExcelSheet
-
-
.Range("C2").Value = rsSmartData("Order ID")
-
.Range("D2").Value = rsSmartData("Orderline ID")
-
.Range("F2").Value = rsSmartData("Product Description")
-
.Range("G2").Value = rsSmartData("Product Name")
-
-
End With
-
-
Excel.Application.ActiveSheet.Protect ("password")
-
-
Excel.Application.DisplayAlerts = False
-
ExcelFile.Save
-
ExcelFile.Close True
-
ExcelApp.Quit
-
-
Set ExcelSheet = Nothing
-
Set ExcelFile = Nothing
-
Set ExcelApp = Nothing
-
-
' Excel call code here
-
-
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
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 - With ExcelSheet
-
-
.Unprotect ("password")
-
.Range("C2").Value = rsSmartData("Order ID")
-
.Range("D2").Value = rsSmartData("Orderline ID")
-
.Range("F2").Value = rsSmartData("Product Description")
-
.Range("G2").Value = rsSmartData("Product Name")
-
.Protect ("password")
-
-
End With
Sign in to post your reply or Sign up for a free account.
Similar topics
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. ...
|
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...
|
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. ...
|
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. ...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |