VBA to Open Attatchments

G

Guest

Hello there,

New to VBA in Outlook, and have a quick question;

There are a number of emails that drop into a joint inbox in my department.
These emails contain excel attachments. I can write some VBA code to deal
with the attachment in excel, but would like to totally automate the process
by writing some VB in Outlook to open up the attachment based on the Email
Subject line??

Would be grateful for a few lines to get me started.

Thanks in advance

Whelan
 
K

Ken Slovak - [MVP - Outlook]

In order to do any manipulation of an Attachment object you need to save it
to the file system. You can use the SaveAs method to do that.

Once you have saved an attachment then manipulating it in code depends on
what type of file the attachment is. If it's a Word file for example you'd
automate Word to open it.

Take a look at the code at
http://www.slovaktech.com/code_samples.htm#StripAttachments for an example
of working with attachments in code.
 
G

Guest

The short version of how to do this:

- call Attachment.SaveAs
- output to a directory of your choice
- use the ShellExecute Win32API function to run the file as if you
double-clicked it

Here's an example:

'Code for the General Declarations section of your module
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String _
, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal
nShowCmd As Long) As Long
Public Const conSwNormal = 1

'Sample calling code
ShellExecute 0, "open", "C:\Temp\Sheet1.xls", vbNullString, "C:\Temp",
conSwNormal

I've written an MSDN article as well that details a similar approach for a
larger solution if you want to see an example in context:

Office Developer Center: Viewing Multiple Picture Attachments in Outlook
2003:
http://msdn.microsoft.com/office/de...ta/html/Office_Outlook_ViewMultPictAttach.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top