Kang
Kang

Reputation: 47

How to attach file in open email?

I want to add an attachment in open email.

Sub AddAttachment() 
    Dim myItem As Outlook.MailItem 
    Dim myAttachments As Outlook.Attachments 
    'Set myItem = Application.CreateItem(olMailItem) 'this will create a new Email which I don't want
    Set myItem= thisEmail **I need help with this part**
    Set myAttachments = myItem.Attachments 
    myAttachments.Add "D:\Documents\Q496.xlsx", olByValue, 1, "4th Quarter 1996 Results Chart" 
    myItem.Display 
End Sub

Upvotes: 0

Views: 182

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

You can use the Inspector.CurrentItem property for retrieving a mail item if it is opened in the inspector window.

Sub AddAttachment() 
     Dim myItem As Outlook.MailItem 
     Dim myAttachments As Outlook.Attachments 
     'Set myItem = Application.CreateItem(olMailItem) 'this will create a new Email which I don't want
     Set myItem= Application.ActiveInspector.CurrentItem
     Set myAttachments = myItem.Attachments 
     myAttachments.Add "D:\Documents\Q496.xlsx", olByValue, 1, "4th Quarter 1996 Results Chart" 
     myItem.Display 
   End Sub

If the mail item is selected in the Explorer window you need to use the Selection object for retrieving the currently selected item in the folder view:

Sub AddAttachment() 
     Dim myItem As Outlook.MailItem 
     Dim myAttachments As Outlook.Attachments 
     'Set myItem = Application.CreateItem(olMailItem) 'this will create a new Email which I don't want
     Set myItem= Application.ActiveExplorer.Selection.Item(1)
     Set myAttachments = myItem.Attachments 
     myAttachments.Add "D:\Documents\Q496.xlsx", olByValue, 1, "4th Quarter 1996 Results Chart" 
     myItem.Display 
   End Sub

Upvotes: 1

Related Questions