当我从 Insert->QuickPart->Document 属性 下拉列表中插入 "built-in" 属性 时,如何在 VBS 中以编程方式复制 Word 的操作?
How can I replicate programmatically in VBS what Word does when I insert a "built-in" property from the Insert->QuickPart->Document Property dropdown?
在 Microsoft Word(Windows 2007 年左右及之后的桌面版)中,我可以使用“插入选项卡”、“文本”组、“浏览快速部件”下拉菜单、“文档 属性”下拉菜单来插入 Content Control 允许 display/entry/selection 多种类型 Document 属性 之一的值,这可能是其中之一以下:
- 内置文档属性
- 与服务器(例如 Microsoft 的 SharePoint 服务器)关联的“内容类型”元属性
如何使用 VBScript 以编程方式插入“内置”文档属性?
以下示例假定您要在 Windows 桌面 Word(即打开的文档)中的当前有效选择处插入映射到 Word 的 built-in 属性之一的内容控件。请参阅以下注意事项。
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
备注:-
“有效选择”是指不会产生单词的选择 error/exception。也就是说,您将需要做更多的事情来避免异常。
此处的描述性(标题、占位符)文本适用于当您将 Word 用户界面语言设置为英语(并且可能只有某些特定的英语版本)时插入这些控件。如果您的界面语言是法语,您可能更愿意为内容控件标题和占位符使用法语等价物。您如何从 Word Object 模型中发现这些文本并不明显。
Word 中有许多不同类型的“属性”,包括
- 内置核心属性
- 内置应用程序属性
- 内置扩展属性
- 内置 CoverPageProps 属性
- 遗留 user-defined 属性(Word 自定义文档属性)
- content-type 属性
- (可以说)Word 文档变量
- 程序员在 Word 自定义 XML 部分中定义 elements/attributes。
您可以使用传统的 Word“字段代码”插入许多这些属性的值 要插入值而不使用 VBA(比方说),您需要一个映射到自定义 [=59= 的内容控件]部分
在 .docx 类型的文档文件(.docx、.docm、.dotx、.dotm)中,内置核心属性、内置应用程序属性和内置扩展属性存储在预定义的 .xml 文件中。例如app.xml等。但是,当Word打开这样的文档时,它会构建两个Word CustomXMLParts。另一个标准自定义 XML 部分包含“CoverPageProps”
的值
您可以将内容控件映射到自定义 XML 部分中的任何元素或属性,包括这些部分。但是 Word 不会在 CustomXML 部分中复制内置文档属性(例如“字数”)的值,这意味着没有内置方法可以插入 Content Control 显示文档中的字数,或 内容控件 显示 User-Defined 文档 属性.
的值
很好的答案。对于那些有兴趣包含其他映射属性(例如来自 SharePoint 的属性)的人,这里有一个 Whosebug post,其中提供了有关如何找到 XML 属性路径的更多详细信息:。
简述:
- 在 SharePoint 库中创建一个 MS Word 文件(包含一些用户列),and/or 在您的文件中加载自定义 XML 属性(参见 here)
- 在word文件中手动插入控件内容到你感兴趣的属性中
- 将 word 文件扩展名更改为 .zip
- 提取存档中包含的 [documentName.docx.zip]\word\document.xml 文档
- 用我最喜欢的 XML 编辑器打开文件
- 然后您可以在以下 element\attributes 找到文档的一般路径:
- 此信息插入上面提供的代码。
注意:可以通过“打开 XML SDK 2.0 生产力工具”访问步骤 1-6 中所述的 XML 内容的完整详细信息,如中所述这个 Whosebug post: How to read metadata information from docx documents?
在 Microsoft Word(Windows 2007 年左右及之后的桌面版)中,我可以使用“插入选项卡”、“文本”组、“浏览快速部件”下拉菜单、“文档 属性”下拉菜单来插入 Content Control 允许 display/entry/selection 多种类型 Document 属性 之一的值,这可能是其中之一以下:
- 内置文档属性
- 与服务器(例如 Microsoft 的 SharePoint 服务器)关联的“内容类型”元属性
如何使用 VBScript 以编程方式插入“内置”文档属性?
以下示例假定您要在 Windows 桌面 Word(即打开的文档)中的当前有效选择处插入映射到 Word 的 built-in 属性之一的内容控件。请参阅以下注意事项。
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
备注:-
“有效选择”是指不会产生单词的选择 error/exception。也就是说,您将需要做更多的事情来避免异常。
此处的描述性(标题、占位符)文本适用于当您将 Word 用户界面语言设置为英语(并且可能只有某些特定的英语版本)时插入这些控件。如果您的界面语言是法语,您可能更愿意为内容控件标题和占位符使用法语等价物。您如何从 Word Object 模型中发现这些文本并不明显。
Word 中有许多不同类型的“属性”,包括
- 内置核心属性
- 内置应用程序属性
- 内置扩展属性
- 内置 CoverPageProps 属性
- 遗留 user-defined 属性(Word 自定义文档属性)
- content-type 属性
- (可以说)Word 文档变量
- 程序员在 Word 自定义 XML 部分中定义 elements/attributes。
您可以使用传统的 Word“字段代码”插入许多这些属性的值 要插入值而不使用 VBA(比方说),您需要一个映射到自定义 [=59= 的内容控件]部分
在 .docx 类型的文档文件(.docx、.docm、.dotx、.dotm)中,内置核心属性、内置应用程序属性和内置扩展属性存储在预定义的 .xml 文件中。例如app.xml等。但是,当Word打开这样的文档时,它会构建两个Word CustomXMLParts。另一个标准自定义 XML 部分包含“CoverPageProps”
的值您可以将内容控件映射到自定义 XML 部分中的任何元素或属性,包括这些部分。但是 Word 不会在 CustomXML 部分中复制内置文档属性(例如“字数”)的值,这意味着没有内置方法可以插入 Content Control 显示文档中的字数,或 内容控件 显示 User-Defined 文档 属性.
的值很好的答案。对于那些有兴趣包含其他映射属性(例如来自 SharePoint 的属性)的人,这里有一个 Whosebug post,其中提供了有关如何找到 XML 属性路径的更多详细信息:
简述:
- 在 SharePoint 库中创建一个 MS Word 文件(包含一些用户列),and/or 在您的文件中加载自定义 XML 属性(参见 here)
- 在word文件中手动插入控件内容到你感兴趣的属性中
- 将 word 文件扩展名更改为 .zip
- 提取存档中包含的 [documentName.docx.zip]\word\document.xml 文档
- 用我最喜欢的 XML 编辑器打开文件
- 然后您可以在以下 element\attributes 找到文档的一般路径:
- 此信息插入上面提供的代码。
注意:可以通过“打开 XML SDK 2.0 生产力工具”访问步骤 1-6 中所述的 XML 内容的完整详细信息,如中所述这个 Whosebug post: How to read metadata information from docx documents?