Alexandru Cimpanu
Alexandru Cimpanu

Reputation: 1069

How do I create and email by taking the adresses from word and having the word as an attachment using visual basic?

I have a Word Document that contains a table with email addresses. I want to get the addresses from the document and and open a Lotus Notes email which is set as default email service having the addresses added to the "To:" field and the document as an attachment. I am already connected to Lotus Notes, I just want the mail to start having the addresses and attachment already in place and not to be sent automatically. I have the code that gets the addresses from table:

Sub Send_mail_recipients()

'NiMo 08-Jun-2013
'Send-mail to distribution list

Dim Text As String
Dim char As String
Dim rowcount, n_address, n_cells, Cell_Crt, CharNo As Integer
Dim Recipient(100) As Variant


'With Application.ActiveWindow.Document
'Activate the Document
'n_address = 0
Text = ""
ActiveDocument.Tables(2).Select
n_cells = Selection.Cells.Count

   For Cell_Crt = 1 To n_cells
    If Selection.Cells(Cell_Crt).Range.Text Like "*@*" Then
        'Recipient(n_address) = Selection.Cells(Cell_Crt).Range.Text
        Text = Text + Selection.Cells(Cell_Crt).Range.Text + ", "
        'n_address = n_address + 1

    End If
   'Text = Selection.Cells(Cell_Crt).Range.Text
   Next

Visual basic provides a method to open a mail havin the document as an attachment:

'If n_address = 0 Then
If Text = "" Then
    myerrmessage = MsgBox("The Document has no email addresses!", vbOKOnly, "error")
Else
    Options.SendMailAttach = True

    ActiveDocument.SendMail

And I found another function that adds the email addresses that I give as parameter to a mail:

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal lpParameters _
As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Private Function OpenEmail(ByVal EmailAddress As String, _
Optional Subject As String, Optional Body As String) _
As Boolean

Dim lWindow As Long
Dim lRet As Long
Dim sParams As String

sParams = EmailAddress
If LCase(Left(sParams, 7)) <> "mailto:" Then _
    sParams = "mailto:" & sParams

If Subject <> "" Then sParams = sParams & "?subject=" & Subject

If Body <> "" Then
    sParams = sParams & IIf(Subject = "", "?", "&")
    sParams = sParams & "body=" & Body
End If

lRet = ShellExecute(lWindow, "open", sParams, _
vbNullString, vbNullString, SW_SHOW)

 OpenEmail = lRet = 0

End Function

OpenEmail Text, "", ""

But I need a way to have both the addresses and the attachment in the same mail.

Upvotes: 0

Views: 685

Answers (1)

RowanC
RowanC

Reputation: 1649

This is the function I've used in the past to do it with notes. Doesn't require passwords etc to be typed afaik.

Sub SendNotesMail(Subject As String, Attachment As String, Recipient As Variant, BodyText As String, SaveIt As Boolean)
'Public Sub SendNotesMail(Subject as string, attachment as string,
'recipient as string, bodytext as string,saveit as Boolean)
'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.

'Set up the objects required for Automation into lotus notes
    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)
    'Start a session to notes
    Set Session = CreateObject("Notes.NotesSession")
    'Next line only works with 5.x and above. Replace password with your password
    '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 or using above password you can use other mailboxes.
    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 = Recipient
    MailDoc.Subject = Subject
    MailDoc.body = BodyText
    MailDoc.SAVEMESSAGEONSEND = SaveIt
    'Set up the embedded object and attachment and attach it
    If Attachment <> "" Then
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment")
'        MailDoc.CREATERICHTEXTITEM ("Attachment")
    End If
    'Send the document
    MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
    MailDoc.SEND 0, Recipient
    'Clean Up
    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set AttachME = Nothing
    Set Session = Nothing
    Set EmbedObj = Nothing
End Sub

Upvotes: 1

Related Questions