Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2020-12-02 21:45:08

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?

Questioner
Profile
Viewed
0
2,935 2020-12-19 00:30:21

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