PDA

View Full Version : Error Trying to send email via Excel



Kal-El
03-06-2017, 10:45 AM
I had a piece of code that was working perfectly, then something happened after my Laptop rebooted and now I am getting an Object Variable with Block Variable not Set

for the life of me I cannot seem to figure out where the issue is coming from

Thoughts ?




Option Explicit

Const EMBED_ATTACHMENT As Long = 1454

Const stPath As String = ""

Const stSubject As String = ""

Const vaMsg As Variant = ""

Sub Send_Active_Sheet()

Dim stFileName As String
Dim vaRecipients As Variant

Dim noSession As Object
Dim noDatabase As Object
Dim noDocument As Object
Dim noEmbedObject As Object
Dim noAttachment As Object
Dim stAttachment As String


stAttachment = stPath & wsOrig.Range("c5").Value & ".xls"

'If Lotus Notes is not open then open the mail-part of it.
If noDatabase.IsOpen = False Then noDatabase.OPENMAIL

'Create the e-mail and the attachment.
Set noDocument = noDatabase.CreateDocument
Set noAttachment = noDocument.CreateRichTextItem("stAttachment")
Set noEmbedObject = noAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)

'Add values to the created e-mail main properties.
With noDocument
.Form = "Memo"
.SendTo = vaRecipients
'.CopyTo = vaCopyTo
.subject = stSubject
.Body = vaMsg
.SaveMessageOnSend = True
.PostedDate = Now()
.Send 0, vaRecipients
End With

'Delete the temporarily workbook.
Kill stAttachment

'Release objects from memory.
Set noEmbedObject = Nothing
Set noAttachment = Nothing
Set noDocument = Nothing
Set noDatabase = Nothing
Set noSession = Nothing

MsgBox "The e-mail has successfully been created and distributed", vbInformation

End Sub

Kal-El
03-06-2017, 11:36 AM
Figured out the error of my ways, somehow the following got deleted

'Instantiate the Lotus Notes COM's Objects.
Set noSession = CreateObject("Notes.NotesSession")
Set noDatabase = noSession.GETDATABASE("", "")

Now new issue is Notes is now crashing on send attempt

Kal-El
03-07-2017, 06:39 AM
The issue is with
.Send 0, vaRecipients

I dumbed it down to a direct link to an already existing file and it works (Below) , So I suspect that it might have something to do with how the stAttachment is being represented in the larger automated process, but I don't see an error anywhere when walking through it

Anyone have any thoughts?




Option Explicit

Const EMBED_ATTACHMENT As Long = 1454

Const stPath As String = "D:\abc123.xls"

Const stSubject As String = "whatever"

Const vaMsg As Variant = "whatever"

Sub Test_send()

Dim stFileName As String
Dim vaRecipients As Variant

Dim noSession As Object
Dim noDatabase As Object
Dim noDocument As Object
Dim noEmbedObject As Object
Dim noAttachment As Object
Dim stAttachment As String

Dim wsOrig As Worksheet
Dim wsTemp As Worksheet
Dim wsNew As Worksheet
Dim wsNew2 As Worksheet
Dim wbNew As Workbook
Dim rng As Range
Dim wsOrig2 As Worksheet


stAttachment = "D:\ABC123.xls"
vaRecipients = "me@me.com"

'Instantiate the Lotus Notes COM's Objects.
Set noSession = CreateObject("Notes.NotesSession")
Set noDatabase = noSession.GETDATABASE("", "")

'If Lotus Notes is not open then open the mail-part of it.
If noDatabase.IsOpen = False Then noDatabase.OPENMAIL

'Create the e-mail and the attachment.
Set noDocument = noDatabase.CreateDocument
Set noAttachment = noDocument.CreateRichTextItem("stAttachment")
Set noEmbedObject = noAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)

'Add values to the created e-mail main properties.
With noDocument
.Form = "Memo"
.SendTo = vaRecipients
'.CopyTo = vaCopyTo
.subject = stSubject
.Body = vaMsg
.SaveMessageOnSend = True
.PostedDate = Now()
.send 0, vaRecipients

End With

'Delete the temporarily workbook.
Kill stAttachment

'Release objects from memory.
Set noEmbedObject = Nothing
Set noAttachment = Nothing
Set noDocument = Nothing
Set noDatabase = Nothing
Set noSession = Nothing

MsgBox "The e-mail has successfully been created and distributed", vbInformation

End Sub