Outlook VBA newItem.Attachments.Add - adding excel files from folder, skipping to next file if first file not available

jobledo

New Member
Joined
Nov 12, 2018
Messages
2
I have a very simple code that I have created in outlook where the code will pick up an email template that was created, open it and add files to the email. I have the designated path for vba to pick and choose the file. My current problem is that sometimes the file in that path/folder may not be available one month but it will be the next. I have searched for hours on how to have the vba attach the first file, and skip the second file if it is not available this month, and go to the next line of code and attach any remaning file that matches the exact file name.

example

file1.xlsx is available this month so vba attaches to the email
file2.xlsx is not available this month so it goes to the next line of code/file and attaches to the email
file3.xlsx attached to email
file4.xlsx attached to email


The end result that files 1,3 & 4 would be attached and any errors from not finding file2 would be eliminated while the available files are attached.
Thank you in advance for any help!



My code is below.



Sub email()
Set newItem = Application.CreateItemFromTemplate("H:\Documents\Custom Office Templates\Outlook\Email file.oft")
newItem.Display

newItem.Attachments.Add "c:\file1.xlsx"
newItem.Attachments.Add "c:\file2.xlsx"
newItem.Attachments.Add "c:\file3.xlsx
newItem.Attachments.Add "c:\file4.xlsx

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi and welcome to the MrExcel board!

You may use this:
If Dir("c:\file2.xlsx") <> "" Then newItem.Attachments.Add "c:\file2.xlsx"

Regards
 
Upvote 0
Vladimir, wow! Thank you! I didn’t realize how hard I was making this. Made me think about other things that I might be making more difficult than they really are ?. This code worked like a charm!
 
Upvote 0
... Made me think about other things that I might be making more difficult than they really are
You are on a good way! And I am glad to show you a short path :biggrin:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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