Is it possible to read and write Content Type Properties in VBA Word 2016 on MacOS?

Viewed 441

I am using a Word dotm file as a template for a SharePoint content type. This word-template contains a Form, opened by a statement in the Document_New() event. A combobox on that form contains the value of a content type property. VBA is used to set the combobox text value via Document.ContentTypeProperties:

Me.cmbComboBox.Text = ThisDocument.ContentTypeProperties("NameOfContentTypeProperty")

This works in Word 2016 on Windows. But in Word 2016 on MacOS this call results in the following error:

Run-time error 5948 This command is not available on this platform.

It seems that this property of the Document object is not available on MacOS.

Does anyone know how to read and write these content type properties in VBA Word 2016 on MacOS?

2 Answers

I managed to read and write the content type properties by editing the XML of the document using the following functions. This works on both Mac and PC.

Function getContentTypeProperty(strElementName As String, docDocument As Word.Document) As String

    Dim xmlNode As CustomXMLNode
    Dim xmlPart As CustomXMLPart

    Set xmlPart = docDocument.CustomXMLParts.SelectByNamespace("http://schemas.microsoft.com/office/2006/metadata/properties").Item(1)

    Set xmlNode = xmlPart.SelectSingleNode("/ns0:properties/documentManagement/ns3:" & strElementName)

    If xmlNode Is Nothing Then
        getContentTypeProperty = ""
    Else
        getContentTypeProperty = xmlNode.Text
    End If

End Function

Function setContentTypeProperty(strElementName As String, docDocument As Word.Document, strValue As String) As Boolean

    Dim xmlNode As CustomXMLNode
    Dim xmlPart As CustomXMLPart

    Set xmlPart = docDocument.CustomXMLParts.SelectByNamespace("http://schemas.microsoft.com/office/2006/metadata/properties").Item(1)

    Set xmlNode = xmlPart.SelectSingleNode("/ns0:properties/documentManagement/ns3:" & strElementName)

    If xmlNode Is Nothing Then
        setContentTypeProperty = False
    Else
        If getAttributeValueByName(xmlNode.Attributes, "nil") = "true" Then setAttributeValueByName xmlNode.Attributes, "nil", "false"
        xmlNode.Text = strValue
        setContentTypeProperty = True
    End If

End Function

Function getAttributeValueByName(xmlAttributes As CustomXMLNodes, strAttributeName As String) As String

    Dim xmlAttribute As CustomXMLNode
    Dim strValue As String

    For Each xmlAttribute In xmlAttributes  
        If xmlAttribute.BaseName = strAttributeName Then strValue = xmlAttribute.NodeValue
    Next

    getAttributeValueByName = strValue

End Function

If the command doesn't exist for Mac you might have to use AppleScript. Unfortunately, I can't help you with AppleScript and I can't find any documentation from MS about what is or isn't available on a Mac.

Here is a compiler directive that will allow you to run different code on Mac or PC.

'Test the conditional compiler constant #Mac
#If Mac Then
'I am a Mac
    Me.cmbComboBox.Text = AppleScriptTask ("MyAppleScriptFile.applescript", "myapplescripthandler", "my parameter string") 
#Else
'I am Windows
    Me.cmbComboBox.Text = ThisDocument.ContentTypeProperties("NameOfContentTypeProperty")
#End If
Related