Results 1 to 3 of 3

Thread: [RESOLVED] [EXCEL] Attach multiple files in Outlook

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    14

    Resolved [RESOLVED] [EXCEL] Attach multiple files in Outlook

    Hi,

    The title may be confusing but I'm using Excel to attach files to an Outlook email.

    Anyway, here's how it goes..

    I have several files in a folder and I want to be able to in a click of a button (Email Reports) will send an email attaching all the files in a folder without having to specify the files one by one.

    Is there a way to do this because I cannot guarantee that the exact file names will be in the folder. It may change from time to time and I just want to automatically attach whatever files are in the folder.

    This is what I have which obviously would not run:

    Private Sub CommandButton1_Click()

    Dim oApp As Outlook.Application
    Set oApp = New Outlook.Application
    Dim oMsg As MailItem
    Dim oAttachment As Outlook.Attachment

    Set oMsg = oApp.CreateItem(olMailItem)

    oMsg.SentOnBehalfOfName = "CSS"
    oMsg.To = ThisWorkbook.Sheets("Sheet1").Range("C15").Value
    oMsg.CC = "CSS"
    oMsg.Subject = ThisWorkbook.Sheets("Sheet1").Range("B6").Value
    oMsg.Body = ThisWorkbook.Sheets("Sheet1").Range("B10").Value

    Set oAttachment = oMsg.Attachments.Add("C:\Documents and Settings\NC\Desktop\test\")

    oMsg.Save

    Set oMsg = Nothing

    Set oApp = Nothing

    End Sub
    Any help will be very helpful!

    Thank you!

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [EXCEL] Attach multiple files in Outlook

    Is this what you are trying?

    Code:
    '~~> Add Reference to MS Outlook xx.xx Library
    Sub Example()
        Dim oOutlook As Outlook.Application, oEmailItem As MailItem
        Dim strDir As String, strFile As String, colFiles As New Collection
        Dim i As Integer
        
        On Error Resume Next
        Set oOutlook = GetObject(, "Outlook.Application")
        If oOutlook Is Nothing Then Set oOutlook = CreateObject("Outlook.Application")
        On Error GoTo 0
        
        '~~> Sample Directory
        strDir = "C:\Documents and Settings\NC\Desktop\test\"
        strFile = Dir(strDir)
        
        '~~> Add all files in that directory to a collection
        While strFile <> ""
            colFiles.Add strFile
            strFile = Dir
        Wend
        
        Set oEmailItem = oOutlook.CreateItem(olMailItem)
    
        With oEmailItem
            '~~> Add files in the collections as attachments
            If colFiles.Count > 0 Then
                For i = 1 To colFiles.Count
                    .Attachments.Add strDir & colFiles(i)
                Next i
            End If
            
            .To = ThisWorkbook.Sheets("Sheet1").Range("C15").Value
            .Subject = ThisWorkbook.Sheets("Sheet1").Range("B6").Value
            .Body = ThisWorkbook.Sheets("Sheet1").Range("B10").Value
            .Display
        End With
    
        Set oEmailItem = Nothing
        Set oOutlook = Nothing
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    14

    Re: [EXCEL] Attach multiple files in Outlook

    That's amazingly correct!

    Thank you so much!

Posting Permissions

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



Click Here to Expand Forum to Full Width