+ Reply to Thread
Results 1 to 3 of 3

Loop through files in folder, email file to listed recipient of file name = cell value

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Loop through files in folder, email file to listed recipient of file name = cell value

    Hi everyone,

    I have a number of files in a single folder. Each of these files must be emailed to unique recipients.

    In my VBA workbook, sheet DataSheet, column P has the file names (without extension), column Q is the To field, and column R is the CC field.

    What I'm trying to do is have a macro which will open file explorer, the user will identify the folder the files are located in, and then loop through each file in the folder. If the file name matches a value in column P, then outlook will send an email with the same file attached to the recipients listed in column Q and R. Below is what I have so far, but I keep getting an error on the Attach.add line.

    Sub Email_Reps()
    Dim OutLookApp As Object
    Dim OutLookMailItem As Object
    Dim lastRow As Long
    Dim MailDest As String
    Dim subj As String
    
    
    lastRow = ThisWorkbook.Worksheets("DataSheet").Cells(Rows.Count, "P").End(xlUp).Row
    
    With Application.FileDialog(msoFileDialogOpen)
    For i = 2 To lastRow
    
        Set OutLookApp = CreateObject("Outlook.application")
        Set OutLookMailItem = OutLookApp.CreateItem(0)
        Set Attach = OutLookMailItem.Attachments
        
        With OutLookMailItem
            .To = Cells(i, 2).Value
            .cc = Cells(1, 3).Value
            .Subject = "Blah"
            .Body = "BlahBlah"
            Attach.Add Cells(i, 1).Value & ".xlsx"
            .Send
        End With
    
    Next
    End With
    
    End Sub
    Any ideas?

    Thanks!!!

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,265

    Re: Loop through files in folder, email file to listed recipient of file name = cell value

    Try this:


    Sub Email_Reps()
        Dim OutLookApp As Object
        Dim OutLookMailItem As Object
        Dim lngR As Long
        Dim MailDest As String
        Dim Attach As Object
        Dim subj As String
        Dim shD As Worksheet
        Dim strPath As String
        Dim strR As String
        Dim oFolder As Variant
        Dim oShell As Variant
    
        Set shD = ThisWorkbook.Worksheets("DataSheet")
        
        'Chose a folder - Show the dialog
        Set oShell = CreateObject("Shell.Application")
        Set oFolder = oShell.BrowseForFolder(0, "Select Folder", 0, "")
    
        If Not oFolder Is Nothing Then
            strPath = oFolder.Self.Path
        Else
            MsgBox "You cancelled."
            Exit Sub
        End If
        
        Set OutLookApp = CreateObject("Outlook.application")
        
        For lngR = 2 To shD.Cells(shD.Rows.Count, "P").End(xlUp).Row
            strR = Dir(strPath & "\" & shD.Cells(lngR, "P").Value & ".xlsx")
            If strR <> "" Then
                Set OutLookMailItem = OutLookApp.CreateItem(0)
                Set Attach = OutLookMailItem.Attachments
    
                With OutLookMailItem
                    .To = shD.Cells(lngR, "Q").Value
                    .cc = shD.Cells(lngR, "R").Value
                    .Subject = "Blah"
                    .Body = "BlahBlah"
                    Attach.Add strPath & "\" & strR
                    .Send
                End With
            End If
        Next lngR
    
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Loop through files in folder, email file to listed recipient of file name = cell value

    Perfect. Thank you.

+ 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. Loop Through all files in folder only finding first file
    By Optional_karl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-02-2016, 07:28 AM
  2. VBA Look Through Folder and Open Files Not Listed in Master File
    By CGriffiths1214 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-07-2015, 12:14 PM
  3. Replies: 3
    Last Post: 01-05-2014, 05:50 AM
  4. [SOLVED] Create hyperlink to file in another folder if listed in column A
    By MusicMan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-05-2013, 04:43 AM
  5. Replies: 1
    Last Post: 11-01-2013, 03:22 PM
  6. Replies: 3
    Last Post: 05-06-2006, 11:50 AM
  7. [SOLVED] .xls email attachments are arriving at the recipient as .dat file.
    By UK-Graham in forum Excel General
    Replies: 4
    Last Post: 02-11-2005, 03:06 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