How can I replicate programmatically in VBS what Word does when I insert a “built-in” property from the Insert->QuickPart->Document Property dropdown?

岁酱吖の 提交于 2020-08-08 07:00:29

问题


In Microsoft Word (Windows desktop version from around 2007 and later), I can use the Insert Tab, Text group, Explore Quick Parts dropdown, Document Property dropdown to insert a Content Control that allows display/entry/selection of the value of one of a number of types of Document Property, which might be one of the following:

  • Built-in document properties
  • "Content Type" metaproperties associated with a server such as Microsoft's SharePoint server

How can I do programmatic insertion of "built-in" document properties using VBScript?


回答1:


The following example assumes that you want to insert a content control mapped to one of Word's built-in properties at a current valid selection in Windows Desktop Word (i.e. in an open document). Please see the following notes.

Option Explicit

' a simple test - assume a document is open in Word
' and you want to insert a "Property Content Control"
' at the current selection

Dim wapp
Set wapp = Getobject(,"Word.Application")
Call insertAndMapProperty(wapp.Selection.Range,"companyfax")
Set wapp = Nothing

Sub insertAndMapProperty(Location, PropertyName) ' As Word.Range, As String
' Location is a Word Range where you want to insert the Content Control
' 
' pass the name of the element (since it does not change when you change the user interface language)

Select Case LCase(Trim(PropertyName))
Case "abstract"
  setCoverPageProps Location, "Abstract", "Abstract"
Case "category"
  setMSCoreProps Location, "category", "Category"
Case "company"
  setExtendedProps Location, "Company", "Company"
Case "contentstatus"
  setMSCoreProps Location, "contentStatus", "Status"
Case "creator"
  setDCoreProps Location, "creator", "Author"
Case "companyaddress"
  setCoverPageProps Location, "CompanyAddress", "Company Address"
Case "companyemail"
  setCoverPageProps Location, "CompanyEmail", "Company E-mail"
Case "companyfax"
  setCoverPageProps Location, "CompanyFax", "Company Fax"
Case "companyphone"
  setCoverPageProps Location, "CompanyPhone", "Company Phone"
Case "description"
  setDCoreProps Location, "description", "Comments"
Case "keywords"
  setMSCoreProps Location, "keywords", "Keywords"
Case "manager"
  setExtendedProps Location, "Manager", "Manager"
Case "publishdate"
  setCoverPageProps Location, "PublishDate", "Publish Date"
Case "subject"
  setDCoreProps Location, "subject", "Subject"
Case "title"
  setDCoreProps Location, "title", "Title"
Case Else
    Wscript.Echo "Unrecognized property name: " & PropertyName 
End Select

End Sub

Sub setCoverPageProps(Location, PropertyName, TitlePlaceHolder)
Const missing = Nothing
Const coverPageMappings = "xmlns:ns0='http://schemas.microsoft.com/office/2006/coverPageProps'"
With Location.ContentControls.Add(1) '1=wdContentControlText
  .Title = TitlePlaceHolder
  .XMLMapping.SetMapping "/ns0:CoverPageProperties[1]/ns0:" & PropertyName & "[1]", coverPageMappings, missing
  .SetPlaceHolderText missing, missing, "[" & TitlePlaceHolder & "]"
End With
End Sub

Sub setDCoreProps(Location, PropertyName, TitlePlaceHolder)
Const missing = Nothing
Const DCoreMappings = "xmlns:ns0='http://purl.org/dc/elements/1.1/' xmlns:ns1='http://schemas.openxmlformats.org/package/2006/metadata/core-properties'"
With Location.ContentControls.Add(1)
  .Title = TitlePlaceHolder
  .XMLMapping.SetMapping "/ns1:coreProperties[1]/ns0:" & PropertyName & "[1]", DCoreMappings, missing
  .SetPlaceHolderText missing, missing, "[" & TitlePlaceHolder & "]"
End With
End Sub

Sub setMSCoreProps(Location, PropertyName, TitlePlaceHolder)
Const missing = Nothing
Const MSCoreMappings = "xmlns:ns0='http://schemas.openxmlformats.org/package/2006/metadata/core-properties'"
With Location.ContentControls.Add(1)
  .Title = TitlePlaceHolder
  .XMLMapping.SetMapping "/ns0:coreProperties[1]/ns0:" & PropertyName & "[1]", MSCoreMappings, missing
  .SetPlaceHolderText missing, missing, "[" & TitlePlaceHolder & "]"
End With
End Sub

Sub setExtendedProps(Location, PropertyName, TitlePlaceHolder)
Const missing = Nothing
Const extendedMappings = "xmlns:ns0='http://schemas.openxmlformats.org/officeDocument/2006/extended-properties'"
With Location.ContentControls.Add(1)
  .Title = TitlePlaceHolder
  .XMLMapping.SetMapping "/ns0:Properties[1]/ns0:" & PropertyName & "[1]", extendedMappings, missing
  .SetPlaceHolderText missing, missing, "[" & TitlePlaceHolder & "]"
End With
End Sub

Notes:-

By "valid selection", I mean a selection that won't cause a Word error/exception. I.e., you will need to do a lot more to avoid exceptions.

The descriptive (Title, Placeholder) texts here are for when you insert these controls with the Word User Interface Language set to English (and possibly only some specific English versions). If your interface language is, say, French, you might prefer to use the French equivalents for the Content Control Title and Placeholder. It is not obvious how you might discover those texts from the Word Object Model.

There are a number of different types of "property" in Word, including

  • builtin core properties
  • builtin app properties
  • builtin extended properties
  • builtin CoverPageProps properties
  • legacy user-defined properties (Word Custom Document Properties)
  • content-type properties
  • (arguably) Word Document Variables
  • programmer defined elements/attributes in Word Custom XML Parts.

You can insert the values of a lot of these properties using traditional Word "Field Codes" To insert values without using VBA (say), you need a Content Control mapped to a CustomXMLPart.

Within a .docx type document file (.docx, .docm, .dotx, .dotm), builtin core properties, builtin app properties, and builtin extended properties are stored in predefined .xml files. e.g. app.xml etc. However, when Word opens such a document, it builds two Word CustomXMLParts. Another standard custom XML part holds the values of the "CoverPageProps"

You can map a Content Control to any element or attribute within a Custom XML Part, including these parts. But Word does not replicate the values of builtin document properties such as "Number of Words", in CustomXML Parts, and that means that there is no builtin way to insert a Content Control that displays the number of Words in a document, or a Content Control that displays the value of a User-Defined Document Property.



来源:https://stackoverflow.com/questions/63236635/how-can-i-replicate-programmatically-in-vbs-what-word-does-when-i-insert-a-buil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!