Solved

Excel VBA - Attach All Files in a Directory

Posted on 2011-09-21
7
700 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

863 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

23 Experts available now in Live!

Get 1:1 Help Now