Consulting

Results 1 to 5 of 5

Thread: Reload this Page Attaching all PDFs in folder to Outlook email using Word VBA

  1. #1

    Reload this Page Attaching all PDFs in folder to Outlook email using Word VBA

    Hi


    I want to attach all PDFs in a specific desktop folder to an Outlook email using VBA in Word


    I'm using below code to generate an Outlook email but cant figure out how to write the attachment part.



    Sub Email_Template1()


    Dim oMailItem As Object, oOLapp As Object

    Dim Word As Object, doc As Object, MsgTxt$

    Set oOLapp = CreateObject("Outlook.Application")

    Set oMailItem = oOLapp.CreateItem(0)

    Set Word = CreateObject("word.application")


    With oMailItem

    .To = "Email"

    .cc = "Email"

    .subject = "Needed Confirmation"

    ' .attachment.Add

    .HTMLBody = "test"

    .Display

    End With


    Set oOLapp = Nothing

    Set oMailItem = Nothing


    End Sub

  2. #2
    VBAX Regular rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    81
    Location
    ​Adding link for Cross-posting LINK​

    ---
    Have a try with your revised macro:
    Option Explicit
    
    Sub Email_Template1()
    
    
        Dim oMailItem As Object, oOLapp As Object
        Dim Word   As Object, doc As Object, MsgTxt$
        Dim filePath As String                        '<=
        Dim fileName As String                        '<=
    
    
        Set oOLapp = CreateObject("Outlook.Application")
        Set oMailItem = oOLapp.CreateItem(0)
        Set Word = CreateObject("word.application")
        With oMailItem
            .To = "Email"
            .Cc = "Email"
            .Subject = "Needed Confirmation"
            '---------------------------------------
            filePath = "F:\Test\Pdf\"               '<= change as needed
            fileName = Dir(filePath & "*.pdf")        '<=
            Do While Len(fileName) > 0                '<=
                .Attachments.Add filePath & fileName  '<=
                fileName = Dir                        '<=
            Loop                                      '<=
            '---------------------------------------
            .HTMLBody = "test"
            .Display
        End With
        Set oOLapp = Nothing
        Set oMailItem = Nothing
        
    End Sub
    Last edited by rollis13; 01-24-2021 at 05:02 PM.

  3. #3
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,310
    Location
    handclips: Kindly read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    I note this is not the first time you've ignored the cross-posting rules you agreed to abide by when joining a forum...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Apologies for the trouble, i did not notice this until now. Will take note going forward.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •