Assign e-mail address based on PDF name from outlook attachment

Cekaay

New Member
Joined
Jan 26, 2018
Messages
2
Hi all,
my name is Constantin and I am mostly guest reading in this forum. Thanks for alle the help so far.
At the moment I am trying set up a macro to send different PDFs automatically to different recipients.
I have a Folder with all pdfs in it and a Excel list with filename and a corresponding E-Mail address. So far I achieved to the following Code:


Code:
Sub send_pdf()
Dim Nachricht As Object, OutlookApplication As Object
Set OutlookApplication = CreateObject("Outlook.Application")
Dim olOldBody As String
Dim Datei As String
Dim folder As String, pfile As String, ext As String
folder = "xxx\ZZZ_Umsätze\"
pfile = Dir(folder & "\*.*")
Do While CBool(Len(pfile))
    ext = Chr(32) & LCase(Trim(Right(Replace(pfile, Chr(46), Space(99)), 99))) & Chr(32)
    If CBool(InStr(1, " pdf ", ext, vbTextCompare)) Then
     Set Nachricht = OutlookApplication.CreateItem(0)
        With Nachricht
            .GetInspector.Display
            .Subject = "turnover 2017"
            olOldBody = .htmlBody
            .Attachments.Add (folder & "\" & pfile)
            .Display
            .To = ""
        End With
    
    End If
    pfile = Dir
Loop
End Sub

By running the Code Outlook automatically opens different E-Mail Messages with a single PDF.

Now I would like to assign a specific E-Mail address, which should be searched based on the PDF filename.
I have tried the following:

Code:
Application.WorksheetFunction.VLookup(FileName, Tabelle1.Range("A1:B350"), 2, False)

Unfortunately it is not working.

Can anybody please help me?

Thanks,
Constantin
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi Constantin

Code:
Sub send_pdf()
' column A contains entries such as "graph theory.pdf"


Dim Nachricht As Object, OutlookApplication As Object
Set OutlookApplication = CreateObject("Outlook.Application")
Dim olOldBody As String, folder$, pfile$, ext$
folder = "c:\pub\client"
pfile = Dir(folder & "\*.*")
Do While CBool(Len(pfile))
    ext = Chr(32) & LCase(Trim(Right(Replace(pfile, Chr(46), Space(99)), 99))) & Chr(32)
    If CBool(InStr(1, " pdf ", ext, vbTextCompare)) Then
        Set Nachricht = OutlookApplication.CreateItem(0)
        With Nachricht
            .GetInspector.Display
            .Subject = "turnover 2017"
            olOldBody = .HTMLBody
            .Attachments.Add (folder & "\" & pfile)
            .Display
            .To = Application.WorksheetFunction.VLookup(pfile, Plan3.[a1:b35], 2, False)
        End With
    End If
    pfile = Dir
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top