Attaching files to Email from Excel via VBA

weaselmcguff

Board Regular
Joined
Feb 24, 2006
Messages
246
Hello,

I am trying to attach multiple files to an automatically generated email. I know what 1 of the files names is going to be. But I am not sure if there are others files and what the names are going to be. I do know where they are located. I want all the files in that sub directory to be attached to the email.

I know how to do multiple files IF I know the names. But in this case I will not know if there are any other files and if there are wont know the name. I do know that they all will have the .JPG extension.

Here is my code for the sending of the email.

With OutMail
If email2 = "" Then
email2 = Application.InputBox("Please enter Email To Automatically Email this To:")
Worksheets("sheet2").Range("a45").Value = email2
Else
.To = email2 ' Who it is going to
.CC = email3 'CC to who it is going to
.Subject = subject1 ' sugbject of email
.Body = msgbody 'template for the body of the email
.Attachments.Add dir2 & thisfile & ".xls" 'dir2 is location where file is. thisfile is name and .xls extension
.Attachments.Add dir2 & "2.jpg" 'here adding the 2.jpg file from the dir2 location
'.Attachments.Add ActiveWorkbook.FullName
.Send 'or use .Display
End If
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing

I can not figure out how to add multiple if any .jpg files to the email

Any suggestions.

Thanks for your time
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I haven't really looked through the rest of your code, but to loop through each .jpg file in the specified folder and attach it to the email, first declare the following variable...

Code:
Dim strFile as String

Then, try replacing...

Code:
.Attachments.Add dir2 & "2.jpg"

with

Code:
strFile = Dir(dir2 & "*.jpg")

Do While Len(strFile) > 0
    .Attachments.Add dir2 & strFile
    strFile = Dir
Loop
 
Upvote 0
I did the changes it is bombing at:

strFile = Dir(dir2 & "*.jpg")

giving me the error:

Compile Error:

Expected Array

Im lost
 
Upvote 0
Dir2 should hold the path (ending with a backslash) to your folder. What value has Dir2 been assigned?
 
Last edited:
Upvote 0
dim dir2 as string

dir2 = Worksheets("data").Range("i8").Value & "\" ' Directory to where it is being saved

this is what dir2 is set to

I get the error with the "dir" part of it

strFile = Dir(dir2 & "*.jpg")
 
Upvote 0
Unfortunately, I see no reason why you would get that error. Maybe the error stems from something else. Which line of code gets highlighted when the error occurs (try stepping through the code, line by line, using F8)?
 
Upvote 0
I tried stepping through the whole code and I error out with the same error at the:

strFile = Dir(dir2 & "*.jpg")

that is what is confusing me. It should be giving me an error. All my variables are defined.

i have not defined the DIR as a variable anywhere does that matter?

strFile = Dir(dir2 & "*.jpg")

Do While Len(strFile) > 0
.Attachments.Add dir2 & strFile
strFile = Dir
Loop
 
Upvote 0
Actually, DIR is a function, not a variable...
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top