Warren Harrington
Warren Harrington

Reputation: 1

Rule with code to save attachments stopped working

I used the script in a rule to save attachments to a folder that exists on my C:\ drive. It ran last week but now doesn't.

Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
sSaveFolder = "C:\Users\warre\Timesheets_IN"
For Each oAttachment In MItem.Attachments
oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
Next
End Sub

The rule searches for a particular phrase in the subject or body of the email on arrival, then runs the script.

I expect all attachments for the relevant emails to be saved in the specified folder.

Upvotes: 0

Views: 583

Answers (2)

David Ricar
David Ricar

Reputation: 1

Same here. Rules worked for a week, moved mails to folders and saved attachments. Now scripts seem to run as the saving window blinks as usual, just the file is not saved, mails are moved by rules. No error, nothing. Basically same script with backslash in directory.

Outlook also simply removed one script that was saved and working, that was the moment it ceased to work, when it threw an error as it tried to run nonexistant script. Since then, saving does not work. Maybe there could be the catch, but I didn't find any useful log.

I solved this trouble by simply copy paste of all the scripts to notepad, removing the vbaproject file from outlook data folder a pasting back from notepad to empty space. Voila, it works!

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

First of all, make sure that your rule is run and script is called.

Then you can try to add a backslash at the end of folder path:

oAttachment.SaveAsFile sSaveFolder & "\" & oAttachment.DisplayName

Or just try to change folder path string in the following way:

Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
  Dim oAttachment As Outlook.Attachment
  Dim sSaveFolder As String
  sSaveFolder = "C:\Users\warre\Timesheets_IN\"
  For Each oAttachment In MItem.Attachments
    oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
  Next
End Sub

Be aware, the DisplayName property value can be the same for attachments in different emails, so the files can be simply overwritten when saved to the disk. Consider adding any IDs to the filename to make it unique. For example, you may consider using the RecievedTime property value to make the file name unique.

Upvotes: 0

Related Questions