Increment variables with a loop

I have an Outlook macro for creating reply to messages.  I will allow users to include up to 5 attachments.  I want to allow the users to modify the part of the vba which has the names of the attachment files which are stored at C:\EmailMsg.

So the users would provide the file names like this:

strAttach1 = "ExpenseReports.pdf"
strAttach2 = "AccountPayableReport.pdf"
strAttach3 = ""
strAttach4 = ""
strAttach5 = ""

But I want to loop while i'm adding the attachments.  Something like this:

For x = 1 To 5
objMail.Attachments.Add ("C:\EmailMsg\" & strAttach & x)
Next x

As I'm sure you know, this results are this this when I debug.print:

C:\EmailMsg\strAttach1
C:\EmailMsg\strAttach2
C:\EmailMsg\strAttach3
C:\EmailMsg\strAttach4
C:\EmailMsg\strAttach5

Instead of:

C:\EmailMsg\ExpenseReports.pdf
C:\EmailMsg\AccountPayableReport.pdf
C:\EmailMsg\
C:\EmailMsg\
C:\EmailMsg\

Any suggestions

August 14th, 2015 12:33am

I ended up going with an array.  Something like this:

Dim myAttach As Variant     For Each myAttach In Array(strAttach1, strAttach2, strAttach3, strAttach4, strAttach5)     objMail.Attachments.Add ("C:\EmailMsg\" & myAttach)     Next myAttach
Free Windows Admin Tool Kit Click here and download it now
August 14th, 2015 3:46pm

Hi John,

Did you resolve your issue?

Regards,

Emi Zhang
TechNet Community Su

August 17th, 2015 2:33am

Hi Emi, I should have been more clear.  Using an array resolved my issue.  Thank you...JD
Free Windows Admin Tool Kit Click here and download it now
August 17th, 2015 8:53am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics