Automatically Save Attachment

  • Thread starter Thread starter March
  • Start date Start date
M

March

Dear All,

I would like to know how to automate saving the attachment while I open
email. I have no idea where to start. I plan to write VBA. I do resecrh on
VBA and try to work on SaveAsFile Method, it doesn't work. Please give me
suggestions.

Thanks,

March
 
I have done a similar vba Code for this:

Dim myItem As Outlook.Inspector
Dim objItem As Object
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.ActiveInspector
If Not TypeName(myItem) = "Nothing" Then
Set objItem = myItem.CurrentItem
strname = "Email sent by" & objItem.SenderName & " Email sent to
records from " & Environ("username")
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "Are you sure you want to save the item? If a file with
the same name already exists, it will be overwritten with this copy of the
file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
objItem.SaveAs "k:\" & strname & ".MSG", olMSG
objItem.FlagIcon = olRedFlagIcon
objItem.Save
MsgBox ("Message Saved")
End If
Else
MsgBox "Open the Email you want to save first."
End If
 
The code at http://www.slovaktech.com/code_samples.htm#StripAttachments
takes all selected emails in a folder and strips the attachments from the
selected items. If you use this code or similar in the Outlook VBA project
just change the line:

Set objOL = CreateObject("Outlook.Application")

to this:

Set objOL = Application

You can modify this code to do what you want.
 
Thanks

Mark said:
I have done a similar vba Code for this:

Dim myItem As Outlook.Inspector
Dim objItem As Object
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.ActiveInspector
If Not TypeName(myItem) = "Nothing" Then
Set objItem = myItem.CurrentItem
strname = "Email sent by" & objItem.SenderName & " Email sent to
records from " & Environ("username")
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "Are you sure you want to save the item? If a file with
the same name already exists, it will be overwritten with this copy of the
file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
objItem.SaveAs "k:\" & strname & ".MSG", olMSG
objItem.FlagIcon = olRedFlagIcon
objItem.Save
MsgBox ("Message Saved")
End If
Else
MsgBox "Open the Email you want to save first."
End If
 
With the following code, I could get the attachments transfer to HardDrive.

Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Att As Attachment
Dim FileName As String

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)

For Each Item In Inbox.Items
For Each Att In Item.Attachments
FileName = "C:\myfolder\" & Att.FileName
Att.SaveAsFile FileName
Next Att
Next Item

However, I want to adrress the Subject name should be "xxxxxxxx" then save
the attachment. I added the condition into the following code.

I run the code, it's been compile error 91 [Object variable or With Block
variable not set]


Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Att As Attachment
Dim FileName As String

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)

Dim Msg As Outlook.MailItem
Set Msg = Item

For Each Item In Inbox.Items
If (Msg.Subject = "Statement") Then
For Each Att In Item.Attachments
FileName = "C:\myfolder\" & Att.FileName
Att.SaveAsFile FileName
Next Att
End If
Next Item

Please give me suggestions.

March
 
If (Item = "Statement") or (Item = "Sub2") Then
........
.......
.........
End If



Above what I've got the answer for myself and I would like to share for
everyone.

March

March said:
With the following code, I could get the attachments transfer to HardDrive.

Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Att As Attachment
Dim FileName As String

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)

For Each Item In Inbox.Items
For Each Att In Item.Attachments
FileName = "C:\myfolder\" & Att.FileName
Att.SaveAsFile FileName
Next Att
Next Item

However, I want to adrress the Subject name should be "xxxxxxxx" then save
the attachment. I added the condition into the following code.

I run the code, it's been compile error 91 [Object variable or With Block
variable not set]


Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Att As Attachment
Dim FileName As String

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)

Dim Msg As Outlook.MailItem
Set Msg = Item

For Each Item In Inbox.Items
If (Msg.Subject = "Statement") Then
For Each Att In Item.Attachments
FileName = "C:\myfolder\" & Att.FileName
Att.SaveAsFile FileName
Next Att
End If
Next Item

Please give me suggestions.

March






March said:
Dear All,

I would like to know how to automate saving the attachment while I open
email. I have no idea where to start. I plan to write VBA. I do resecrh on
VBA and try to work on SaveAsFile Method, it doesn't work. Please give me
suggestions.

Thanks,

March
 
Back
Top