我使用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中读写这些内容类型属性?
通过使用以下功能编辑文档的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
如果这是您问题的答案,请用绿色的对勾标记。这表明问题已得到回答,并给您分数!