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.
***************************************************
***************************************************
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.