Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


April 22, 2002

Working with Attachments


RSS
Subscribe to Windows IT Pro | See More Scripting Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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.

End of Article

   Previous  1  2  [3]  Next  


Reader Comments
Fantastic Article. I just needed a little help with VBA inside Outlook. (Never mind the WSH and fso which confused me.) This article helped me do everything I needed to do = Open Multiple Excel Attachments in Outlook, and they must be done within the same instance of Excel (something which "Call Shell" did not do).

Now I can write an Excel Macro to move the sepearate files from the Window menu into separate worksheets in a single workbook.

Thanks. Very Very Good Article

John Voris March 06, 2003


Easy to understand simple task require simple code and sometimes it's not easy to find! Thanks

Jensen Lodge October 30, 2003


Great
helpfull

jpsechao December 17, 2003


i wud like to know about working with .msg type attachments.

gaurav December 30, 2003


i know there is a mailto command that allows you to attach a file - is there a similar command that allows you to detach?

basically what i want to do is send a file attachment to someone and then give them the option of automatically forwarding the email but without any of the original text but just the attachment

george jackson January 13, 2004


Outlook application opens and I am able to send an attachment, however I am unable to add more than one line to the body (text of the msg)

regards



laxmi karnik March 05, 2004


Very useful document but with one question i have a macro to create a task from an email which copies the email but not the attachments. Is there any way to do this?

Peter Duckett June 29, 2004


gaurav, you didn't say what you wanted to do with the .msg attachments. If you want to create a copy, try the Namespace.GetItemFromTemplate method, which can take an .msg file path as an argument. Or, check out the Import method available in the Redemption library from http://www.dimastr.com/redemption/

Sue_Mosher August 19, 2004 (Article Rating: )


Peter, see http://www.outlookcode.com/d/code/copyatts.htm for code that shows how to copy attachments.

Sue_Mosher August 19, 2004 (Article Rating: )


laxmi, you can add as much text as you want to a message.

Sue_Mosher August 19, 2004 (Article Rating: )


 See More Comments  1   2 

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Is Microsoft Just Like IBM?

Microsoft has defined the way we do business from a technology perspective for years. But with a younger generation that lives in the clouds, is Microsoft's recent progress in cloud computing too little, too late? ...

Microsoft, News Corp. Discuss Locking Out Google

Microsoft and Rupert Murdoch's News Corp. recently discussed an alliance that would counter Google's fledgling online news service. ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Take Control of Your Email: Understand the Business Reasons for Email Storage Management

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

Bail Out Your Exchange Environment

Continuous Application Virtualization: An Answer to Exchange Recovery Problems

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement