Forum

VBA Code to attach ...
 
Notifications
Clear all

VBA Code to attach .jpg to body of Outlook email

6 Posts
2 Users
0 Reactions
200 Views
(@crainaud)
Posts: 35
Trusted Member
Topic starter
 

Hi,

I've successfully been using the VBA code (see attached sample file) to attach an excel file to an Outlook email.  Using 2010 Excel 2010 and Outlook.

In the body of the email, I wish to insert a small word doc or a .jpg file (whichever is easier) instead of the contents in cell B5.

How do I update the VBA code? 

I've search the internet, but the code found is beyond my skill level.

All help is greatly appreciated.  Thanks and have a great day. 

 
Posted : 02/12/2017 11:12 am
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Carolyn

Try changing this line of your code from xlsx to jpg/docx etc

If InStr(1, AttachFile, "docx", vbTextCompare) Then

In cell B4 change the file name.

You can also do away with the If...ENDIF and only use  .Attachments.Add AttachFile

In this way, it will not check on the type of file but will send whatever you entered in cell B4.

NOTE

I think you should not put your actual e-mail address on the test file.

Otherwise you will be flooded with e-mails when we test your codes Laugh

Hope this helps.

Sunny

 
Posted : 02/12/2017 8:26 pm
(@crainaud)
Posts: 35
Trusted Member
Topic starter
 

Hi Sunny,

Apologies for not getting back to you sooner!

Your advise was helpful.  I was able to figure out how to add the .jpg to the body of the email along with attaching an excel file.

Attached is the file with the VBA and the Mail worksheet set-up.  May be of help to others.

I have one last question, you mentioned "You can also do away with the If...ENDIF and only use  .Attachments.Add AttachFile".  My code still has the filter for "xls".  Would you be so kind as to let me know what the code should be to remove the filter, lines 25:29?

Thanks again!

Carolyn

 
Posted : 08/12/2017 1:10 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Carolyn

I was referring to this (lines 30-32):

If InStr(1, AttachFile, "xls", vbTextCompare) Then
    .Attachments.Add AttachFile
End If

Here you are only attaching files with the xls extensions and will ignore others.

I was suggesting you remove the lines in red.

BTW there is no attachment in your post #3

Cheers

Sunny

 
Posted : 08/12/2017 8:00 pm
(@crainaud)
Posts: 35
Trusted Member
Topic starter
 

Hi Sunny,

Your answer did the trick!

Let's try that again... attaching the file with the VBA code and "dummy" email address.

Thanks for your patience and help on this.  Have a great weekend.

Carolyn

 
Posted : 09/12/2017 1:16 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Carolyn

Thank you for your feedback.

Glad that we could help.

Cheers

Sunny

 
Posted : 09/12/2017 11:11 pm
Share: