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

其他-是否可以在MacOS上的VBA Word 2016中读写内容类型属性?

(其他 - Is it possible to read and write Content Type Properties in VBA Word 2016 on MacOS?)

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

我使用Word dotm文件作为SharePoint内容类型的模板。该单词模板包含一个表单,该表单由Document_New()事件中的语句打开该窗体上的组合框包含内容类型属性的值。VBA用于通过以下方式设置组合框文本值Document.ContentTypeProperties

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

这适用于Windows上的Word 2016。但是在MacOS上的Word 2016中,此调用导致以下错误:

运行时错误5948此命令在此平台上不可用。

看来Document对象的此属性在MacOS上不可用。

有谁知道如何在MacOS的VBA Word 2016中读写这些内容类型属性?

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

通过使用以下功能编辑文档的XML,我设法读写了内容类型属性。在Mac和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