problem running excel macro in module

presuming_ed

Registered User.
Local time
Today, 01:22
Joined
May 6, 2003
Messages
23
I have the following module which runs an excel macro and mails the resulting spreadsheet.

***************************************************
***************************************************

Public Function SendNotesMail(strSendTo As String, strBody As String, strSubject As String)
'This public sub will send a mail and attachment if neccessary to the recipient including the body text.
'Requires that notes client is installed on the system.

'Create the Excel Workbook and run Excel Macro to reformat ready for mailing
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strsql As String
Dim objxl As Excel.Application
Dim objxlwrkbk As Excel.Workbook
Dim objxlwrksht As Excel.Worksheet
DoCmd.OutputTo acOutputTable, "email_this", acFormatXLS, "C:\stock_tracker.xls", False

strsql = "select * from email_this"

Set db = CurrentDb
Set rs = db.OpenRecordset(strsql)

Set objxl = CreateObject("Excel.Application")
Set objxlwrkbk = objxl.Workbooks.Open("c:\stock_tracker.xls")
Set objxlwrksht = objxlwrkbk.Worksheets("email_this")
With rs
objxlwrksht.Rows("1:1").Select
With Selection
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
End With

..... lots of excel macro code that isn't important...........
objxlwrksht.Range("A15").Select
Selection.copy
objxlwrksht.Range("A1").Select
ActiveSheet.Paste
Rows("13:13").Select
Selection.Delete Shift:=xlUp
End With

objxlwrkbk.Close savechanges:=True
objxl.Quit

DoCmd.SetWarnings True
'Set up the objects required for Automation into lotus notes
Dim Subject As String
Dim Attachment As String
Dim recipient As String
Dim BodyText As String
Dim SaveIt As Boolean
Dim Maildb As Object 'The mail database
Dim UserName As String 'The current users notes name
Dim MailDbName As String 'THe current users notes mail database name
Dim MailDoc As Object 'The mail document itself
Dim AttachME As Object 'The attachment richtextfile object
Dim Session As Object 'The notes session
Dim EmbedObj As Object 'The embedded object (Attachment)
Dim EmailSend As Object
Dim EmailApp As Object

'Start a session to notes

Set Session = CreateObject("Notes.NotesSession")

'Get the sessions username and then calculate the mail file name.
'You may or may not need this as for MailDBname with some systems you can pass an empty string
UserName = Session.UserName
MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"

'Open the mail database in notes
Set Maildb = Session.GetDatabase("", MailDbName)
If Maildb.IsOpen = True Then
'Already open for mail
Else
Maildb.OPENMAIL
End If

'Set up the new mail document
Set MailDoc = Maildb.CreateDocument
MailDoc.Form = "Memo"
MailDoc.sendto = strSendTo
MailDoc.Subject = strSubject
MailDoc.Body = strBody
MailDoc.SaveMessageOnSend = SaveIt

'Set up the embedded object and attachment and attach it

'If Attachment <> "" Then
Set AttachME = MailDoc.CreateRichTextItem("C:\stock_tracker.xls")
Set EmbedObj = AttachME.EmbedObject(1454, "", "C:\stock_tracker.xls")
'End If

'Send the document
MailDoc.send 0, recipient

'Clean Up
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj = Nothing
Kill ("C:\stock_tracker.xls")

End Function

***************************************************
***************************************************

The first time the module is executed it runs ok but when it is subsequently called it fails with error message on the first
Selection.Insert Shift:=xlDown line

Run Time Error 91
Object variable or with block variable not set.

According to Access help it's probably something to do with a variable not being set, but I can't see it myself. I just think it's a bit strange that it works the first time!

Can anyone help? (you can probably ignore most of the macro coding)

Thanks.
 
Hi,
I'd say this is bacause you don't clean up your Excel objects.
Maybe if you set these to Nothing in the Clean up section it'll sort it out.

HTH,
Patrick.
 
Thanks for the suggestion Patrick. I tried it but still got the same problem. One thing that may be worth mentioning is that I've included the Excel 8.0 object library references, but when I create the first spreadsheet I get a messsage from excel saying that the spreadsheet was created using a different version of Excel. Is this significant?
 
Hi,
I'm not really a user of Excel objects so I can't really answer your second query. But thinking logically, if the code runs the first time around without error, then in order for the code to run correctly the second time, the machine must be in the same state as when the code first ran.

Am I making sense?
I.e. You need to identify what is different the second time around & what you need to do to reset it to it's initial state.
Sorry I can't be of more help,
Patrick.
 
R u sure Xl has actually shut down in windows.
ctrl alt del
and check if its still there after the first macro run.
I had a problem with XL staying open in windows for a while.It shows under processes if it still there not applications
Just an idea !!
 

Users who are viewing this thread

Back
Top Bottom