1
votes

I'm having difficulty getting Outlook 2010 VBA to recognize a macro that I copied directly from the web. The macro is supposed to extract a copy of an attachment from an e-mail message and save it to a local folder. It worked several days ago, but now, when I try to run the code in VBA with (itm as Outlook.MailItem), Outlook doesn't recognize it as any kind of macro. When I take that out and leave just parentheses, it shows as a macro, but I get a 424 object required error and can't run the macro. It's damned if I do and damned if I don't.

Here's the original code:

Public Sub saveAttachtoDisk(itm as Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder as String

saveFolder = "c:\temp\"
    For Each objAtt In itm.Attachments

        objAtt.SaveAsFile savefolder & "\" & objAtt.DisplayName

        Set objAtt = Nothing

    Next

End Sub

I've tried everything, and nothing seems to work. Any ideas?

2
If a macro has a parameter, you can't call it without passing a valuefor that parameter (unless it's marked as optional). How are you trying to run this macro?Tim Williams
How are you calling this sub? One thing I notice - although it's not connected - is your SaveAsFile location will resolve to something like: c:\temp\\MyAttachment - with two backslashes between the folder and displayname.Doug Glancy
I just tested calling your code from another subroutine, and the double backslashes don't keep it from working. The attachments are saved just fine. Interesting, I thought two backslashes would make it fail.Doug Glancy
@TimWilliams, what do you mean how am I trying to run it?GregSpev
@DougGlancy, when you run it does it give you another, more expanded code? That's what happened the other day. Any idea as to why that would happen?GregSpev

2 Answers

1
votes

Since the code includes a parameter, you need to pass that parameter by calling this procedure from another procedure. Ex:

Sub CallMyProcedure()

Dim itms As Outlook.Items
Dim itm As Object

' loop through default Inbox items
Set itms = Session.GetDefaultFolder(olFolderInbox).Items

For Each itm In itms
  If TypeName(itm) = "MailItem" Then
    ' your code is called here
    saveAttachtoDisk itm
  End If
Next itm

End Sub

This is the procedure you would need to call, and since it has no parameters you should be able to see it in the macros dialog.

1
votes

"What would that code look like? Where would it go?"

You'd open the Outlook Visual Basic Editor, by clicking Alt-F11, and paste this code over the previous version of your macro. It's probably currently inside "This Outlook Session."

You can run it from inside the Macro Dialog as you've been trying to do. Or you can attach it to a ribbon button by clicking File>Options>Customize Ribbon. You also have to set your Macro Security, in the Developer tab, to "Notifications for all Macros." It will ask you to confirm it's safe to run the first time you click it in any given Outlook session.

Public Sub saveAttachtoDisk()

Dim saveFolder As String
Dim objAtt As Outlook.Attachment
Dim itm As Outlook.MailItem

saveFolder = "c:\temp\"
For Each itm In ActiveExplorer.Selection
    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
        Set objAtt = Nothing
    Next objAtt
Next itm
End Sub

This sub saves the attachments in all the selected items in the currently open folder. You could adjust it to just do all the items in the currently selected folder, or a static folder, such as the Inbox. You'll need to familiarize yourself with the Outlook object model to do so. JimmyPena's answer gives an example of doing it with all the Inbox items.