简体   繁体   中英

outlook attach file in opened Email

I like to create a macro that will add an attachment in opened 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```

Thank you very much

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM