PDA

View Full Version : Simple VBA code to Add Attachment to Active Message



scwebb
06-08-2016, 01:23 AM
Hi all,

I have written some macros for work that allows the user to insert product information into the body of an email. What I would like is for the macro to also add the relevant spec sheet for the product as an attachment (pdf).

I assume I need to use the "attachments.add" method but (not being much of a coder) I can't figure out exactly what to do. I have found various examples online of how to add an attachment to a new message, but I can't figure out how to modify it to add the attachment to my active message.

Currently my code looks something like this:

Public Sub test()
Dim Ins As Outlook.Inspector
Dim Document As Word.Document
Dim Word As Word.Application
Dim Selection As Word.Selection

Set Ins = Application.ActiveInspector
Set Document = Ins.WordEditor
Set Word = Document.Application
Set Selection = Word.Selection

Selection.TypeText Text:="Product"
Selection.TypeParagraph
Selection.TypeText Text:="Product Code"
Selection.TypeParagraph
Selection.TypeText Text:="Price"
Selection.TypeParagraph
Selection.TypeText Text:="Delivery"
Selection.TypeParagraph
Selection.TypeParagraph

End Sub


I would appreciate it if anyone could advise on how to use the attachments.add method with this code.

Thanks.

gmayor
06-08-2016, 03:03 AM
You need to set a reference to the current message that you are editing e.g.


Dim oMsg as Outlook.MailItem
Set Ins = Application.ActiveInspector 'Existing line
Set oMsg = Ins.CurrentItem 'set a reference to the current message
oMsg.Attachments.Add "c:\Path\Filename.pdf" ' Add the PDF to the message

scwebb
06-08-2016, 03:54 AM
You need to set a reference to the current message that you are editing e.g.


Dim oMsg as Outlook.MailItem
Set Ins = Application.ActiveInspector 'Existing line
Set oMsg = Ins.CurrentItem 'set a reference to the current message
oMsg.Attachments.Add "c:\Path\Filename.pdf" ' Add the PDF to the message

Thanks for that, it works perfectly. Setting the reference was the part I didn't have my head around so this has been very helpful.