Hi,
I have very little knowledge of VBA but have managed to scrape my way through to getting the end product I need. Basically Within a sheet I have email addresses and within cells B9, B11, B13 there is a file path to a file that will attach when the vba is executed.
The problem is that sometimes a file attachment will not be required and when the cell values in either/all cells B9, B11, B13 are blank the vba script will not run because the error states that there is no file to attach.
Is there a way for teh script to check if there is a file location, if not then skip it to the next file and so on until it sends just a blank email if required.
Hope that makes sense. Code is below. Main issues are around the
Set obAttachment = noDocument.CreateRichTextItem("stAttachment")
Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
Full Code is here:
Sub SendEmailGroups()
Dim noSession As Object, noDatabase As Object, noDocument As Object
Dim obAttachment As Object, EmbedObject As Object
Dim stSubject As Variant, stAttachment As String
Dim vaRecipient As Variant, vaMsg As Variant
Const EMBED_ATTACHMENT As Long = 1454
Const stTitle As String = "Active workbook status"
Const stMsg As String = "The active workbook must first be saved " & vbCrLf _
& "before it can be sent as an attachment."
'Check if the active workbook is saved or not
'If the active workbook has not been saved at all.
If Len(ActiveWorkbook.Path) = 0 Then
MsgBox stMsg, vbInformation, stTitle
Exit Sub
End If
'If the changes in the active workbook have been saved or not.
If ActiveWorkbook.Saved = False Then
If MsgBox("Do you want to save the changes before sending?", _
vbYesNo + vbInformation, stTitle) = vbYes Then _
ActiveWorkbook.Save
End If
bccRecipient = Range("A3:A100").Value
'vaRecipient = Range("D2").Value
vaMsg = Range("G2").Value
stSubject = Range("F2").Value
stAttachment = Range("B9").Value
stAttachment2 = Range("B11").Value
stAttachment3 = Range("B13").Value
' stAttachment = ActiveWorkbook.FullName
'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 obAttachment = noDocument.CreateRichTextItem("stAttachment")
Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
Set obAttachment = noDocument.CreateRichTextItem("stAttachment2")
Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment2)
Set obAttachment = noDocument.CreateRichTextItem("stAttachment3")
Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment3)
'Add values to the created e-mail main properties.
With noDocument
.Form = "Memo"
' .SendTo = vaRecipient
.BlindCopyTo = bccRecipient
.Subject = stSubject
.Body = vaMsg
.SaveMessageOnSend = True
End With
'Send the e-mail.
With noDocument
.PostedDate = Now()
.Send 0, vaRecipient
End With
'Release objects from the memory.
Set EmbedObject = Nothing
Set obAttachment = Nothing
Set noDocument = Nothing
Set noDatabase = Nothing
Set noSession = Nothing
'Activate Excel for the user.
AppActivate "Microsoft Excel"
MsgBox "The e-mail has successfully been created and distributed.", vbInformation
End Sub
Hope someone can help!
Thanks
Bookmarks