问题
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?
回答1:
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.
回答2:
"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.
来源:https://stackoverflow.com/questions/11870717/outlook-vba-macro-not-recognized