2
votes

I have VBA code to look for attachments in e-mails in a specific Outlook folder and save them into a location.

I have an Outlook rule that sends the emails to the Outlook folder then fires the macro. The code does not fire on my computer automatically, but it does on another computer. The email comes in and the rule drops it into the folder, but the code does not run. When I run the code manually, through the VB Editor, it saves the attachment in the right place.

I have checked to make sure folder and path names are correct (either way if they were wrong the code would not run properly when I run it manually).

Outlook rule:

Apply this rule after the message arrives

from xxxxxxxxxxxx

and with xxxx in the subject

and on this machine only

move it to the xxxxx folder

and run Project1.ThisOutlookSession.Save

VBA code:

Sub Save(item As Outlook.MailItem)
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim msg As Outlook.MailItem

Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("xxxxxxx")
Set oApp = CreateObject("Shell.Application")

For Each myitem In olFolder.Items
  For Each att In myitem.Attachments
    att.SaveAsFile "\\.............\" & att.FileName
      myitem.Attachments.Remove 1
      myitem.Save
  Next
Next
End Sub
2
Try this stackoverflow.com/questions/6604675/… move the message in the code as shown in this post. So basically you combine your code and my code.Siddharth Rout

2 Answers

0
votes

The incoming mailitem is item in Sub Save(item As Outlook.MailItem). It is not used in the code.

Later when run manually the code still ignores whatever item is passed but all items in olFolder are now processed.

Try this:

Sub Save(item As Outlook.MailItem)
For Each att In item.Attachments
    att.SaveAsFile "\\.............\" & att.FileName
    item.Attachments.Remove 1
    item.Save
Next
End Sub
0
votes

try deleting the file (save the codes somewhere else first) ... \Documents and Settings\\Application Data\Microsoft\Outlook\VbaProject.OTM