微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 VBA 检索 Outlook 附件文档属性

如何解决使用 VBA 检索 Outlook 附件文档属性

我试图从附件中的 Word/Excel 文档中检索一些内置(甚至是自定义?)属性,而无需保存临时文件并在相应的应用程序中打开它们。我尝试将它们附加在 MailItemDocumentItem 中。

我感兴趣的属性是:作者、标题、LastSaveDtm 等。Outlook 似乎能够获取它们,因为作者姓名出现在 DocumentItem 的预览窗格的顶部。

我能找到的获取这些属性的唯一方法是使用 here 中描述的以下方法

varProp = MailItem.PropertyAccessor.GetProperties(SchemaName)
varProp = DocumentItem.PropertyAccessor.GetProperties(SchemaName)

varProp = MailItem.Attachments(1).PropertyAccessor.GetProperties(SchemaName)
varProp = DocumentItem.Attachments(1).PropertyAccessor.GetProperties(SchemaName)

SchemaName 在此处定义:[MS-OXPROPS]: Exchange Server Protocols Master Property List

规范中的一些有趣定义:

我尝试检索的属性没有与 proptag 命名空间一起使用的 MAPI 标记语法(规范名称,如 PidTagPropName) (根据我测试过的内容*)也没有与 id 命名空间一起使用的 MAPI id 语法(规范名称,如 PidLidPropName),但是只有 MAPI 字符串语法(规范名称,如 PidNamePropName)与 string 命名空间一起使用。

这是我为 SchemaName 尝试的:

"http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/Author"
"http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/Title"
"urn:schemas-microsoft-com:office:office#Author"
"urn:schemas-microsoft-com:office:office#Title"

它们都不起作用。

This document 表示“命名属性由客户定义,有时由服务提供商定义”

我还发现文档属性应该“在 MAPI 中自动发布”。

那么我做错了什么?

(*) SchemaName 的工作 (PidTagSubject):

  • "http://schemas.microsoft.com/mapi/proptag/0x0037001E"
  • "http://schemas.microsoft.com/exchange/subject-utf8"

解决方法

Outlook 对象模型和 Redemption 都没有为此提供任何属性或方法。

您需要将附件保存在磁盘上,然后从文件中检索文档属性。 Attachment.SaveAsFile 方法将附件保存到指定路径。

Sub SaveAttachment() 
 Dim myInspector As Outlook.Inspector 
 Dim myItem As Outlook.MailItem 
 Dim myAttachments As Outlook.Attachments
 
 Set myInspector = Application.ActiveInspector
 If Not TypeName(myInspector) = "Nothing" Then
   If TypeName(myInspector.CurrentItem) = "MailItem" Then 
     Set myItem = myInspector.CurrentItem 
     Set myAttachments = myItem.Attachments 
     'Prompt the user for confirmation 
     Dim strPrompt As String 
     strPrompt = "Are you sure you want to save the first attachment in the current item to the Documents folder? If a file with the same name already exists in the destination folder,it will be overwritten with this copy of the file." 
     If MsgBox(strPrompt,vbYesNo + vbQuestion) = vbYes Then 
       myAttachments.Item(1).SaveAsFile Environ("HOMEPATH") & "\My Documents\" & myAttachments.Item(1).DisplayName 
     End If 
   Else 
     MsgBox "The item is of the wrong type." 
   End If 
  End If  
End Sub

注意,您可以自动化 Office 应用程序,您可以在其中打开保存的文档并以编程方式读取属性。有关详细信息,请参阅 How to use a single VBA procedure to read or write both custom and built-in Document Properties

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。