Attaching Contents of a Folder

Hi all,

Using a macro running on excel, I am trying to attach the contents of a folder in Outlook. The files contained in the folder change, so it is not as simple as writing the code to attach a specific file. I need it to attach all the files regardless of the file name. Is this possible

Thanks in advance



Answer this question

Attaching Contents of a Folder

  • Paul_wa

    Hi,

    Yes it's possible.

    To include a reference to the Outlook library use the menu, within VBE, Tools > References... Browse for Microsoft Outlook library.

    Sub EmailAttachFiles()
    '
    ' Currently using Late binding.
    ' Include reference to Outlook Library to remove Objects
    '
    Dim olApp As Object 'Outlook.Application
    Dim olMail As Object 'Outlook.MailItem
    Dim strPath As String
    Dim strFile As String

    Set olApp = CreateObject("Outlook.Application")
    olApp.Session.Logon
    Set olMail = olApp.CreateItem(0)

    On Error Resume Next

    With olMail
    .To = "x@x.x"
    .CC = ""
    .Subject = "<Subject line>"
    .Body = "<Body Message>"
    .Display 'to auto send use .Send

    strPath = "C:\Temp\Design_1_files\"
    strFile = Dir(strPath & "*.*")
    Do While strFile <> ""
    .Attachments.Add strPath & strFile
    strFile = Dir
    Loop

    End With

    Set olMail = Nothing
    Set olApp = Nothing

    End Sub



  • Indinfer

    Have you tried the code with early binding. That is removing the Object from the declaration lines and using the Outlook objects.

    This requires you to include a reference to the Outlook library.
    In VBE use Tools > References... and then scan down the list to Microsoft Outlook....

    If that does not fix it them I'm stumped as to why the code does not function for you.


  • fDogGT

    Hey Andy,

    When I tried to run your code I got this error message, "The specified module could not be found".

    How do I fix it

    Best Regards

    Cathrine


  • R.Tutus

    Hi Catherine,

    Not 100% why you should get that message.

    I assume you have Outlook and not another email client.

    You could try a modification to this line.
    Set OutApp = CreateObject ("Outlook.Application", "localhost")


  • fddsfsdf

    Thanks Andy, works a treat
  • Bobo1234

    Andy

    I am using Outlook as email client.

    I have tried your solution, but it still not working.

    Cath


  • Attaching Contents of a Folder