运行 Outlook 中用于将附件从共享收件箱保存到磁盘的宏

Run macro in Outlook to save attachment to disk from shared inbox

我可以完全访问我帐户中的两个共享电子邮件。我想 运行 一个共享电子邮件收件箱中的一个宏,用于将附件保存到硬盘。我不希望宏 运行 作用于收件箱中的所有项目,而只作用于 selected/highlighted。我无法让下面的代码工作。我可以得到一些关于如何使我的代码工作的建议吗?

Public Sub saveAttachtoDisk ()

Dim objAtt As Outlook.Attachment
Dim dateFormat
    dateFormat = Format(Now, "yyyy-mm-dd H-mm")
Dim saveFolder As String
Dim itm As Outlook.MailItem

Dim objNS As Outlook.NameSpace
Set objNS = olApp.GetNamespace("MAPI")

Dim myRecipient As Outlook.Recipient
Set myRecipient = objNS.CreateRecipient("invoices@domain.com")
myRecipient.Resolve
Set inbox = objNS.GetSharedDefaultFolder(myRecipient, olFolderInbox)

saveFolder = "c:\temp\"

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

代码看起来不错,我没有看到任何奇怪的地方。但很可能您需要更正文件路径(删除双反斜杠):

objAtt.SaveAsFile saveFolder & dateFormat & objAtt.DisplayName

代码有没有错误?您是否尝试指定另一个文件路径?

请注意,C: 驱动器需要管理员权限才能在启用 UAC 的系统上写入。

Option Explicit    ' <-----

Public Sub saveAttachtoDisk()
...
End Sub

Compile error:
Variable not defined

olApp 未定义:

Set objNS = olApp.GetNamespace("MAPI")

如果代码在 Outlook 中:

Set objNS = Application.GetNamespace("MAPI")

如果不在 Outlook 中:

Dim olApp As Outlook.Application

在新模块的顶部自动生成Option Explicit

在 VB 编辑器中。工具菜单 |选项

勾选"Require Variable Declaration"