PDA

View Full Version : Solved: Copying attachments



mdmackillop
03-08-2007, 10:29 AM
I'm trying to use the following code from the Help file, but MyInspector is not being set. I'm running this from Outlook, so I'm not sure that I need to create the new application, but I'm new to this!


Sub SaveAttachment()
Dim myOlApp As Outlook.Application
Dim myInspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments

Set myOlApp = CreateObject("Outlook.Application")
Set myInspector = myOlApp.ActiveInspector
If Not TypeName(myInspector) = "Nothing" Then
If TypeName(myInspector.CurrentItem) = "MailItem" Then
Set myItem = myInspector.CurrentItem
Set myAttachments = myItem.Attachments
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "Are you sure you want to save the first attachment in the current item to the C:\ folder? If a file with the same name already exists in the destination folder, it will be overwritten with this copy of the file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
myAttachments.Item(1).SaveAsFile "M:\" & InputBox("Job No.") & "\Attachments\" & _
myAttachments.Item(1).DisplayName
End If
Else
MsgBox "The item is of the wrong type."
End If
End If
End Sub

Norie
03-08-2007, 10:41 AM
Don't know about Inspector but this is the code I use to copy attachments to my hard drive.

Sub SaveAttachments()
Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Dim myAttachment As Outlook.Attachment
Dim I As Long

Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myFolder = myFolder.Folders("Job Stuff")

For Each myItem In myFolder.Items
If myItem.Attachments.Count <> 0 Then
For Each myAttachment In myItem.Attachments
I = I + 1
myAttachment.SaveAsFile "C:\MailTest\Attachment" & I & ".csv"
Next
End If

Next
End Sub

mdmackillop
03-08-2007, 11:36 AM
Thanks Norie, I'll give that a try.

Rocket
07-22-2008, 05:06 PM
My attempts to duplicate these solutions has failed. Obviously, I need some special assistance. please

safwanjamil
08-01-2008, 12:57 AM
The Code did not work for me either. I'm using MS Office 2003.

~Jamil