+ Reply to Thread
Results 1 to 4 of 4

Macro to attach all files in a folder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,776

    Macro to attach all files in a folder

    I have a macro to create an email and attach a workbook, which works perfectly to attach the active workbbook


    However, I need the macro amended so that it attaches all the csv files in the folder "C:\sales JNLS\

    Your assistance is most appreciated




     Sub Email_filesInFolder()
       ThisWorkbook.Activate                           'start in THIS workbook
    ztext = [bodytext]                              'read in text from named cell
    Zsubject = [subjectText]
    
      Dim OutApp As Object
        Dim OutMail As Object
    Dim Strpath As String
    Dim Strfile As String
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
          With OutMail
    
          
                .To = Join(Application.Transpose(Sheets("Data").Range("AU1:AU2").Value), ";")
    
                .CC = ""
                
                .BCC = ""
                .Subject = Zsubject
               .Body = ztext
              
    
    
                
              .Attachments.Add ActiveWorkbook.FullName
                
               .Display
              End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
       
        
    End Sub
    Attached Files Attached Files
    Last edited by Howardc1001; 11-26-2019 at 03:39 PM.

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to attach all files in a folder

    I only tested this for compile errors not for run.
    Sub Email_filesInFolder()                          'start in THIS workbook
    ztext = [bodytext]                              'read in text from named cell
    Zsubject = [subjectText]
    Dim fName As String
    Dim OutApp As Object
    Dim OutMail As Object
    Dim Strpath As String
    Dim Strfile As String
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
            With OutMail
               .To = Join(Application.Transpose(Sheets("Data").Range("AU1:AU2").Value), ";")
               .CC = ""
               .BCC = ""
               .Subject = Zsubject
               .Body = ztext
               fName = Dir("C:\sales JNLS\*.csv")
                    Do While fName <> ""
                        .Attachments.Add "C:\sales JNLS\" & fName
                        fName = Dir
                    Loop
               .Display
            End With
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,776

    Re: Macro to attach all files in a folder

    Thanks for the help. I have tested the code and it works perfectly

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to attach all files in a folder

    Quote Originally Posted by Howardc1001 View Post
    Thanks for the help. I have tested the code and it works perfectly
    You're welcome,
    regards, JLG

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Attach files created by macro to an email
    By JesseSingh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-04-2016, 12:12 AM
  2. Outlook macro to attach most recently created file from folder
    By lsargent in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 08-17-2015, 11:45 AM
  3. [SOLVED] Loop Through Folder, Create Emails with Sub Folder Names in Subject, Attach files in sub
    By Rschwar23 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2015, 10:06 AM
  4. Help with macro not working - Pull from fields > attach doc from folder > send email
    By mikey386 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2015, 03:42 PM
  5. Excel Email Macro - HELP! - Need to be able to attach two different files
    By benwahchang in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-29-2013, 08:41 AM
  6. Replies: 1
    Last Post: 03-12-2013, 04:45 AM
  7. [SOLVED] Macro to loop through workbooks in folder and attach and email using address in closed WB
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-03-2013, 07:23 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1