Add Method (Attachments Collection) Example

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

This Visual Basic for Applications example uses CreateItem to create a new mail message, attaches a Microsoft Excel workbook as an attachment (not a link) using the Attachments property and gives the attachment a descriptive caption.

  Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.CreateItem(olMailItem)
Set myAttachments = myItem.Attachments
myAttachments.Add "C:\My Documents\Q496.xls", _
    olByValue, 1, "4th Quarter 1996 Results Chart"

This Visual Basic for Applications example creates a new mail message and attaches a Microsoft Word document from a server using a link.

  Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.CreateItem(olMailItem)
Set myAttachments = myItem.Attachments
Set myAttachment = myAttachments.Add _
"\\MYSVR1\Reports\Q496Report.doc", _
    olByReference

This Visual Basic for Applications example creates a new mail message and attaches the first contact in the default Contacts folder.

  Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNameSpace("MAPI")
Set myFolder = _
    myNameSpace.GetDefaultFolder(olFolderContacts)
Set myFirstContact = myFolder.Items(1)
Set myItem = myOlApp.CreateItem(olMailItem)
Set myAttachments = myItem.Attachments
myAttachments.Add myFirstContact

This example shows how to perform the same action using VBScript.

  Set myNameSpace = Application.GetNameSpace("MAPI")
Set myFolder = _
    myNameSpace.GetDefaultFolder(10)
Set myFirstContact = myFolder.Items(1)
Set myItem = Application.CreateItem(0)
Set myAttachments = myItem.Attachments
myAttachments.Add myFirstContact