Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1107
  • Last Modified:

Excel VBA - Attach All Files in a Directory

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
AndresHernando
Asked:
AndresHernando
  • 3
  • 3
1 Solution
 
jmdl1983Commented:
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
 
NorieCommented:
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
 
AndresHernandoAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
NorieCommented:
Add this in the loop:

strFileName = Dir

It will repeat the Dir and return the next file to attach.
0
 
AndresHernandoAuthor Commented:
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
 
NorieCommented:
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
 
AndresHernandoAuthor Commented:
imnorie,  sorry for the slow response.  Your code works great!   Thanks!!!  --Andres
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now