Subscribe to Windows IT Pro
April 22, 2002 12:00 AM

Working with Attachments

Windows IT Pro
InstantDoc ID #24423
Rating: (7)

The basic component of Scripting Runtime is FileSystemObject, which provides methods to access files and folders. Let’s say you want to save an Outlook message attachment as a file. First, check whether a file already exists with the name you want to use. FileSystemObject provides a FileExists method to give you that information. The code

Dim fso As New Scripting.FileSystemObject
Set objAtt = objMsg.Attachments(1)
strFileName = "C:\Temp\" & objAtt.FileName
If Not fso.FileExists(strFileName) Then
    objAtt.SaveAsFile strFileName
End If

saves the first attachment in a MailItem object (i.e., Outlook message) named objMsg only if the desired file name doesn’t already exist. If you later want to delete the saved file, you can use the FileSystemObject’s simple DeleteFile method:

fso.DeleteFile strFileName

FileSystemObject doesn’t have a method for opening an attached file directly from inside a message. When a user double-clicks an Outlook-item attachment to open it, Outlook saves a temporary copy of the file to the user’s system, then opens that copy. If you want to open attachments programmatically, you need to do the same thing—save a copy locally, then open the saved file.

You’ve already seen how to use the SaveAsFile method to save a message attachment as a file. To open it, you use another feature of WSH that lets you write a procedure that works similarly to double-clicking a file. When you double-click a file, Windows checks the file extension (e.g., .doc, .exe) to see what program to use to open the file. An .exe file is a program, so Windows just runs it. A .doc file is a Microsoft Word document, so Windows runs Word and opens the .doc file in Word.

WSH includes a Shell object that supports a Run method that can run any file without your specifying the program with which the file is associated. Listing 1 shows a RunFile subroutine that you can call from other procedures to open any file on your system. Note the error-handling code that deals with cases in which the file has no associated program.

Opening File Attachments
Now, we can put some of these techniques together into a useful macro that opens all the attachments in the message you’ve selected in a folder. The OpenAllFiles subroutine in Listing 2 loops through all the attachments in the item, saving each to a local folder, then calling the RunFile subroutine in Listing 1 to open the file. Note that OpenAllFiles doesn’t check first to see whether a file by the same name exists.

I’ve just scratched the surface of working with Outlook-item attachments, but you now know how to attach a file or Outlook item programmatically and how to save attached files and open those saved files. You’ve also learned about WSH and its FileSystemObject, which can help you work with files and folders in your Outlook VBA macros.

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

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