Link to home
Start Free TrialLog in
Avatar of commfirst
commfirst

asked on

Word Macro to create PDF then attach PDF to email with address from merged data

I cannot figure out how to put dynamic data from the SQL Server datasource to the Outlook email address field. I've got two days on this and so close.

CONTEXT: My ladies on the front desk email reservations to people. It's a rediculously slow process. Here is my VBA that is almost complete to make it where they just run macro and then hit send.

I can't get the email address with field name "GuestEMail" to the .To attribute. I do not know VBA but I've pieced together many macros to get this far. I know the organization of the code is atrocious.

SEE VIDEO for more understanding: https://goo.gl/YKqGq0

Sub PPMagic_MOVE_N_SAVE0()
    Selection.Copy
    ChangeFileOpenDirectory "C:\files\PPMagic\"
    ActiveDocument.SaveAs2 FileName:=Trim(Selection.Text) & ".doc", FileFormat _
        :=wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
        True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
        False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=0

    Dim strData As String
    Dim strFileName As String
    
    strData = "C:\files\PPMagic\" & Trim(Selection.Text) & ".pdf"
    
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        strData, ExportFormat:= _
        wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentWithMarkup, IncludeDocProps:=False, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
        On Error Resume Next
        
strData = "C:\files\PPMagic\" & Trim(Selection.Text) & ".pdf"

Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim myAttachments As Object
Dim strEmail As String

Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
Set myAttachments = OutLookMailItem.Attachments


With OutLookMailItem
    .To = GuestEMail
    .Subject = "Your James W. Smith Reservation"
    '.Body = "Thank you for booking with us. Here is your reservation." & vbNewLine & signature
    myAttachments.Add (strData)
    '.Send
    .Display
End With
    signature = OutLookMailItem.Body

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing
        
End Sub

Open in new window

Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try

.To = activedocument.MailMerge.DataSource.DataFields("GuestEMail").Value

Regards
Avatar of commfirst

ASKER

That didn't work. I'm googling and trying variations. I'll see if that will put the email in the body. Just thought of that.

Sorry for delay and thanks for your time.
ASKER CERTIFIED SOLUTION
Avatar of commfirst
commfirst

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm guessing I wasn't a good question writer. I figured it out on my own because no one helped. No biggy. I love figuring this stuff out. Just don't always have the time to do it.