Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

April 22, 2002 12:00 AM

Working with Attachments

Windows IT Pro
InstantDoc ID #24423
Rating: (7)
Add, save, and open Outlook-item attachments

Many common Outlook operations center on attachments—adding them to messages and other items, opening them, and saving them as system files. Let’s explore how Outlook programmers work with attachments and look at the Scripting Runtime programming library, which comes in handy when working with file attachments.

Adding an Attachment
Every Outlook item, except the yellow-sticky-note NoteItem object, supports an Attachments collection. Like other collections, the Attachments collection supports an Add method to attach a new file, an Item method to help enumerate the attachments in the collection, and a Remove method to delete an attachment from the item. However, the Remove method doesn’t work in all versions of Outlook, so I recommend avoiding it.

When you attach a file to an Outlook item, you must provide either the path to a file on your system or an Outlook item object, such as a ContactItem or MailItem. Other parameters are optional. Unfortunately, the Help file for Outlook 2002 displays the details of the AddressEntries.Add method instead of the Attachments.Add method. You can find information about Attachments.Add at http://support.microsoft.com/support/office/inprodhlp/outlook/olmthaddattachmentsobj.asp, but this Web page doesn’t tell the whole story.

The basic syntax for adding a file as an attachment to a new Outlook message looks like this:

Set objOL = _
    CreateObject("Outlook.Application")
Set objMsg = _
    objOL.CreateItem(olMailItem)
Set objAttachments = _
    objMsg.Attachments
Set objAtt = _
    objAttachments.Add("c:\myfile.doc")

Related Content:

ARTICLE TOOLS

Comments
  • Zuber
    7 years ago
    Jul 13, 2005

    i am trying to attach jpg files, but can't. It used to work earlier before i updated Outlook.
    I extract the path of the jpg files from hyperlinks in my access database. could that be the problem. It attaches other .txt files.

    please help

  • Anonymous User
    7 years ago
    Mar 08, 2005

    The set objAtt line forces a run-time 424 error 'object required'. The author does not address this or any other setup requirements that would prevent this - making the code useless. The article is either incomplete or contains errors.

  • Aveesh
    7 years ago
    Feb 01, 2005

    great article, sue!

    i have one question - i receive emails that have been forwarded like a milion times (jokes mostly). The goal is to keep opening attachments which are messages until there are no more. the last message opened may have picture attachments or none (only body). Is the only way to savefile and then open the attachment using S***** and keep doing so? is there no "doublelick" method akin to double clicking the attched message within the mailitem?

    Regards,
    aveesh***ar

  • Sam Greenhough
    7 years ago
    Jan 04, 2005

    I am trying to strip out and save an attachment. The attachment is an excel file which is itself within an attached .msg file. I set up a routine to firstly save the .msg file to temp folder. I then need to open it and strip out and save attached excel file. However WshShell.Run will not open files of type .msg. ShellExecute will, but the shell command is asynchronous - the macro will not wait for the file to open before trying to save the attachment - big problem! How do you force the macro to wait or the shell command to be synchronous, or is there any other way to achieve this?

  • mal4mac
    8 years ago
    Sep 17, 2004

    Check out this great tutorial on using Microsoft Outlook for managing your ebay & amazon sales, or use it as an Outlook programming quickstart:

    http://www.321books.co.uk/ebooks/outlook-vba-tutorial.htm

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.