Solved

Excel VBA - Attach All Files in a Directory

Posted on 2011-09-21
7
836 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 34

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 34

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

738 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