Solved

Excel VBA - Attach All Files in a Directory

Posted on 2011-09-21
7
730 Views
Last Modified: 2012-05-12
What's the code to attach all files in a directory?

This is what I have...

            With OutMail
                .To = Range("Rde_Reviewer2_GatekeeperEmail").Value
                .CC = Range("Rd_Reviewer2ReqCC_Email").Value
                .Subject = "Unit Review of LOGCAP CO: " _
                    & v_ReqName & " - " _
                    & Range("c_db1_PPRnum").Value
                .DeliveryReceiptRequested = True
                .OriginatorDeliveryReportRequested = True
                .Body = strBody

                '//put code here to attach all files in c:\MainDir\SubDir1\SubDir2\*.*
                .Attachments.Add    ????

            End With

Thanks, --Andres
0
Comment
Question by:AndresHernando
  • 3
  • 3
7 Comments
 
LVL 4

Expert Comment

by:jmdl1983
ID: 36573994
Sub openAllfilesInALocation()
Dim i as integer, wb as workbook
With Application.FileSearch
..NewSearch
..LookIn = "c:\MainDir\SubDir1\SubDir2\"
..SearchSubFolders = False
..FileName = "*.*"
..Execute
For i = 1 To .FoundFiles.Count
 'Put Your Attach Code Here Now
Next i
End With
End Sub
0
 
LVL 33

Expert Comment

by:Norie
ID: 36574009
Try this.
strPath = "C:\MainDir\SubDir1\Subdir2\"
strFileName = Dir(strPath & "*.*")

Do While Len(strFilename)>0

       .Attachments.Add strPath & strFileName

Loop

Open in new window

0
 

Author Comment

by:AndresHernando
ID: 36579221
imnorie, your code is very close...  However, it's an infinite loop that attaches the first file in the dir over and over.

Is there a line we can insert to "kill"  (delete) each file as it is attached?

Thanks, --Andres
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 33

Expert Comment

by:Norie
ID: 36579625
Add this in the loop:

strFileName = Dir

It will repeat the Dir and return the next file to attach.
0
 

Author Comment

by:AndresHernando
ID: 36580709
imnorie, I added the line of code "strFileName = Dir" (see below) but still have same result:  first file in Dir gets attached over and over in an infinite loop.

What line in the code gets Excel to look for the next file?

    strPath = v_DirSaveTo
    strFileName = Dir(strPath & "*.*")
    i = 0
   
    Do While Len(strFileName) > 0
        .Attachments.Add strPath & strFileName
        strFileName = Dir
        strFileName = Dir(strPath & "*.*")

        i = i + 1
        If i > 15 Then Exit Do
    Loop

Thanks, --Andres
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 36580898
Why did you repeat the initial Dir in the loop?

It's not needed and will actually reset the Dir back to the first file.

This is the whole loop.
Do While Len(strFileName) > 0
        .Attachments.Add strPath & strFileName
        strFileName = Dir
Loop

Open in new window

0
 

Author Comment

by:AndresHernando
ID: 36596989
imnorie,  sorry for the slow response.  Your code works great!   Thanks!!!  --Andres
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now