I’m new to this so am having a hard time just “starting the engine”. I have looked at several code snippets, but am not quite getting the gist of what needs to be done here to save attachments from emails in my inbox or a subfolder to the file system. I’m trying to launch this by assigning a macro to the code in outlook’s default VBA: Module1
I tried variation 1 – but this doesn’t even let me create a macro since i have “MItem As Outlook.MailItem” in the Sub definition. Not sure what that is the case.
Public Sub SaveAtt(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
' Set the folder where attachments will be saved
sSaveFolder = "C:TempjunkMove" ' Change this path to your desired folder
For Each oAttachment In MItem.Attachments
oAttachment.SaveAsFile sSaveFolder & oAttachment.FileName
Next oAttachment
End Sub
Next I remove that Variable passed into the subroutine, but then I get a debug message: Obejct variable or with block not set. See attached JPG imageenter image description here
As a side note, I was originally trying to run this routine with the intent of selecting an individual mailbox item in my inbox, clicking the macro button and then letting it rip. I’m not sure if that is possible. So my next thought is to move all the interesting mailbox items to a subfolder of the inbox and running the routine once (from a macro launch button) on all messages in this folder.
I found another piece of code, which also looks like it may work, but it doesn’t.
Public Sub SaveAtt()
Dim MItem As MailItem
Dim oAttachment As Attachment
Dim sSaveFolder As String
Dim oDefInbox As Folder
Dim targetFolder As Folder
Dim myItems As Outlook.Items
Dim Item As Object
Set oDefInbox = Session.GetDefaultFolder(olFolderInbox)
'Set targetFolder = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("Inbox")
sSaveFolder = "C:TempjunkMove"
For Each MItem In oDefInbox.Items
For Each oAttachment In MItem.Attachments
oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
Set oAttachment = Nothing
Next oAttachment
Next MItem
End Sub
Any ideas, what am I doing wrong here? I think the main problems im having are setting up the outlook namespace/object and/or calling this subroutine from the macro button.
thx in advance, W
two pieces of prebuilt code. see above for the code snippets.