Working with Attachments

     

If you want to work with files attached to a message, use the MailItem object's Attachments property. This returns the collection of Attachment objects for the message. For each Attachment object, you can manipulate the following properties and methods :

Attachment .DisplayName ” Returns the name below the icon for the specified Attachment .

Attachment .Filename ” Returns the filename of the specified Attachment .

Attachment .Delete ” Deletes the specified Attachment .

Attachment .SaveAs ” Saves the specified Attachment to disk:

 Attachment.SaveAs(Path) 

Attachment

The Attachment object you want to save.

Path

The path and filename to which you want to save the file.

Listing 11.5 shows a procedure that creates a forwarded message and removes all the attachments before sending it.

Listing 11.5. A Procedure That Creates a Forwarded Message and Deletes Any Existing Attachments Before Sending the Message
 Sub ForwardAndDeleteAttachments()     Dim ns As NameSpace     Dim ib As MAPIFolder     Dim msg As MailItem     Dim att As Attachment     '     ' Set up the namespace and Inbox     '     Set ns = ThisOutlookSession.Session     Set ib = ns.GetDefaultFolder(olFolderInbox)     '     ' Create the forwarded MailItem     '     Set msg = ib.Items(ib.Items.Count).Forward     With msg         '         ' Delete all the attachments         '         For Each att in .Attachments             att.Delete         Next 'att         '         ' Send it (change the address!)         '         .Recipients.Add "blah@yadda.com"         .Send     End With End Sub 

To add an attachment to an outgoing message, use the Attachments object's Add method:

  MailItem  .Attachments.Add(  Source, Type, Position, DisplayName  ) 

MailItem

The MailItem object to which you want to add the attachments.

Source

The path and filename for the attachment.

Type

(optional) A constant that specifies what kind of attachment you want to send:

 

olByValue

Sends the attachment as is (this is the default).

 

olByReference

Sends the attachment as a link to the original file.

 

olEmbeddedItem

Sends the attachment as a link to an Outlook item.

Position

(optional) The position of the attachment within the message body. Use 1 to place the attachment at the beginning of the message; use any value n to position the attachment before the n th character in the message.

DisplayName

(optional) The name that appears below the attachment icon if Type is olByValue .

You can run the Add method as many times as you like for the same MailItem.



Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net