【问题标题】:Retrieve Outlook Attachment Document Properties using VBA使用 VBA 检索 Outlook 附件文档属性
【发布时间】:2021-04-24 19:15:11
【问题描述】:

我正在尝试从附件中的 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

规范中的一些有趣定义:

  • 命名属性:由 GUID 和字符串名称或 32 位标识符标识的属性
  • Document 对象:表示单个文件的 Message 对象,例如由文字处理应用程序生成的文档。这 消息对象包含文件作为附件对象并包括 用于描述文件的其他属性。

我尝试检索的属性没有与proptag 命名空间(其中根据我测试过的内容工作*),也没有与id 命名空间一起使用的 MAPI id 语法(规范名称,如 PidLidPropName),但只有 MAPI id syntax strong>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"

【问题讨论】:

  • 没有理由猜测 - 只需使用 OutlookSpy 查看现有的 DocumentItem(单击 IMessage 按钮)。这些属性均未设置。
  • 我无法安装 OutlookSpy。为什么他们没有设置?这不是他们的目的吗?
  • 不保证存在 MAPI 属性。
  • 好吧,如果你想到另一种不需要救赎的方式,我全都听好了!

标签: vba outlook properties namespaces mapi


【解决方案1】:

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 了解更多信息。

【讨论】:

  • 我帖子的第一句话:“不使用 Excel 或 Word”...
  • 这只是一个选项。例如,如果您只处理打开的 XML 文档,您可以考虑使用Open XML SDK 或任何不需要在系统上安装 Office 应用程序的第三方组件。
  • @hymced 由您决定如何读取已保存文件的内容。没有必要为此自动化 Office 应用程序。
  • 是的,我知道:stackoverflow.com/a/67059653/2981328 但我说我不想(因为我不能在我的情况下)保存文件。
  • 没有其他方法可以适用于所有情况。没有人可以保证这些属性(在帖子中提到)是在 Outlook 项目上设置的。最稳定的解决方案是保存文件,然后处理它们。或者只是处理表示附加项的字节流。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-12
  • 2014-10-25
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
相关资源
最近更新 更多