0

I am attempting to retrieve some built-in (even custom?) properties from Word/Excel documents in attachments without saving temp files and opening them in the respective Application. I tried attaching them in a MailItem and a DocumentItem.

The properties I am interested in would be: Author, Title, LastSaveDtm, etc. Outlook seems to be able to get them, because the Author name appears at the top of the Preview Pane of a DocumentItem.

The only way I could find to get those properties is using the following methods as described 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)

The SchemaName's are defined in here: [MS-OXPROPS]: Exchange Server Protocols Master Property List

Some interesting definitions from the specs:

  • named property: A property that is identified by both a GUID and either a string name or a 32-bit identifier
  • Document object: A Message object that represents a single file, such as a document generated by a word-processing application. The Message object contains the file as an Attachment object and includes additional properties to describe the file.

The properties I am trying to retrieve do not have a MAPI tag syntax (canonical name like PidTagPropName) to be used with the proptag namespace (which works from what I have tested*) nor a MAPI id syntax (canonical name like PidLidPropName) to be used with the id namespace, but only have a MAPI string syntax (canonical name like PidNamePropName) to be used with the string namespace.

Here is what I tried for the 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"

None of them works.

This document says that "named properties are defined by clients and occasionally service providers"

I have also seen nomewhere that document properties should be "automatically published in MAPI".

So what I am doing wrong?

(*) SchemaName's that work (PidTagSubject):

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

1 Answers1

-1

The Outlook object model, nor Redemption, doesn't provide any property or method for that.

You need to save attachments on the disk and then retrieve document properties from files. The Attachment.SaveAsFile method saves the attachment to the specified path.

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

Note, you can automate Office applications where you can open the saved document and read the properties programmatically. See How to use a single VBA procedure to read or write both custom and built-in Document Properties for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • first sentence of my post: "without using Excel or Word"... – hymced Apr 24 '21 at 11:40
  • It is just an option. For example, if you deal with open XML documents only, you may consider using [Open XML SDK](https://learn.microsoft.com/en-us/office/open-xml/open-xml-sdk) or any third-party components that doesn't require Office application installed on the system. – Eugene Astafiev Apr 24 '21 at 13:21
  • @hymced it is up to you how to read the content of saved files. It is not necessary to automate Office applications for that. – Eugene Astafiev Apr 24 '21 at 13:22
  • yes indeed, I know that: https://stackoverflow.com/a/67059653/2981328 but I said that I don't want (because I can't in my situation) to save the file. – hymced Apr 24 '21 at 13:28
  • There is no other way which can work for every case. Nobody can guarantee that these properties (mentioned on the post) are set on the Outlook item. The most stable solution is to save files and then process them. Or just process the stream of bytes that represents the attached item. – Eugene Astafiev Apr 24 '21 at 13:45
  • if you know how to process the stream of bytes that would be wonderful ! – hymced Apr 24 '21 at 13:48
  • Try to use `IMAPIProp::OpenProperty(PR_ATTACH_DATA_BIN, IIS_IStream, ...)`. See [PropertyAccessor.GetProperty( PR_ATTACH_DATA_BIN) fails for outlook attachment](https://stackoverflow.com/questions/40164807/propertyaccessor-getproperty-pr-attach-data-bin-fails-for-outlook-attachment) for more information. – Eugene Astafiev Apr 24 '21 at 13:54
  • yes I have already seen that, do you know how to call the method of the interface you mention in VBA without using Redemption? – hymced Apr 24 '21 at 13:56
  • That's entirely separate question, right? – Eugene Astafiev Apr 24 '21 at 14:03
  • mmm no, it's not, if it solves my initial issue with attachment properties without saving the attachments. – hymced Apr 24 '21 at 14:56