PDA

View Full Version : Macro to save attachment to folder not running



catalystsyst
12-06-2018, 04:05 PM
I have a macro set to run when a rule identifies a new email with a keyword in the subject. For some reason, it is no longer running, even when I run it manually. When I have tried "Public Sub SaveEmailAttachmentsToFolder(item As Outlook.MailItem)" nothing happens when I run the macro.

When I declare "item as Outlook.MailItem" as Public after Option Explicit, the code will run but it errors out. The macro also no longer shows under "Run Script" in the rule.

I have tried placing the code in Outlook Session and in a Module. I know I'm missing something but cannot figure out what it is.

Option Explicit
Public Sub SaveEmailAttachmentsToFolder(item As Outlook.MailItem)
Dim I As Integer
Dim Inbox As MAPIFolder
Dim Atmt As Outlook.Attachment
Dim strFileName As String
Dim strDestFolder As String
On Error GoTo ThisMacro_err
strDestFolder = "c:\Symphony Development\import\"
For Each Atmt In item.Attachments
strFileName = Atmt.DisplayName
If LCase(Right(strFileName, 3)) = "csv" Or LCase(Right(strFileName, 4)) = "xlsm" Or LCase(Right(strFileName, 4)) = "xlsx" Then
Atmt.SaveAsFile strDestFolder & strFileName
End If
Next
ThisMacro_exit:
Set Inbox = Nothing
Exit Sub
''''''''''''''''''''''''''''''''''''''''''''
ThisMacro_err:
End Sub

skatonni
12-18-2018, 11:00 AM
You have to pass a mailitem.


Sub Maiitem_Parameter_For_SaveEmailAttachmentsToFolder()

' first open an appropriate mailitem
SaveEmailAttachmentsToFolder ActiveInspector.currentItem

End Sub



Assuming your code runs now. It is possible running code from rules has been disabled. Google it for ways to re-enable. If not allowed you can switch to ItemAdd or NewMaiEx.