通过 SharePoint 中的 VBA 自定义文档属性添加 MS Word [英] MS Word adding via VBA Custom Document Properties from SharePoint

查看:70
本文介绍了通过 SharePoint 中的 VBA 自定义文档属性添加 MS Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现自己无法在 Word 中添加控制内容对象,这些对象链接到链接到 SharePoint 库的文档属性,在 SharePoint 库中创建新列时会公开这些属性.

我发布了我最初的问题:MSWord 通过 VBA 添加自定义文档属性(来自 SharePoint)作为 ContentControl.不幸的是,我无法在那里发布我的解决方案,因为有些人认为我的问题不完整.

幸运的是,一位用户 (@slightlysnarky) 好心地发布了一个解决方案来解决我的部分问题 如何在 VBS 中以编程方式复制 Word 在插入内置"文件时的作用插入->快速部件->文档属性下拉列表中的属性?.

解决方案

以下是我将内容控件链接到从 SharePoint 库继承的文档属性的方法(针对用户定义的列给出了示例).

>

从@slightlysnarky 提供的解决方案中,我不清楚如何找到文档属性的 .XMLMapping.SetMapping 路径.

为了找到这些信息,我做了以下事情:

  1. 在我的 SharePoint 库中创建一个 MS Word 文件(包含一些用户列)
  2. 在word文件中手动插入控制内容(见原问题)
  3. 保存文件
  4. 将 word 文件扩展名更改为 .zip
  5. 提取存档中包含的[documentName.docx.zip]\word\document.xml文档
  6. 使用我最喜欢的 XML 编辑器打开文件
  7. 然后您可以在以下 element\attributes 中找到文档的一般路径:
    • 就我而言,我有: w:prefixMappings=""xmlns:ns0='http://schemas.microsoft.com/office/2006/metadata/properties' xmlns:ns1='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns2='http://schemas.microsoft.com/office/infopath/2007/PartnerControls' xmlns:ns3='856dd977-5561-4031-9d6b-b2809bca48df'"
    • w:prefixMappings 属性对于所有属性都是相同的.它可能会因不同的库而变化(待验证)
    • w:xpath 属性对于每个属性都是不同的,并且与我可以在文档XML Map"中找到的信息相匹配.(请参阅屏幕截图的原始问题).
    • 我也意识到,不幸的是,一旦在 SharePoint 中创建了列,无论 SharePoint 中的名称是否更改,XML 映射中的名称都不会更改,这是可以预料的.因此,您可以在下面的代码中看到,在某些情况下,我的属性映射与最终 SharePoint 中所需的给定名称不同.经验教训是,最好在创建库列之前仔细考虑命名约定.
  8. 根据这些信息,我将@slightlysnarky 提供的代码修改为普通模板中的 VBA 宏,一切正常.

为了帮助他人,这里是我改编自@slightlysnarky 的代码.请注意,您需要根据您的库设置进行调整,如上所述.* 更改为:Sub setSharepointProps()* 您需要更改:从 document.xml 中提取的 sharePointPropsMappings=[rootOfProperties].XMLMapping.SetMapping [pathToProperties](见上文)

<预><代码>' 一个简单的测试 - 放置在普通的 .dotm 文件 VBA 内容中' 或任何您希望代码驻留的位置.' 为了快速测试运行 test() 子.它将插入一个映射控件' 文档中的内容Sub insertAndMapProperty(Location, PropertyName) ' As Word.Range, As String' 位置是您要插入内容控件的字词范围'' 传递元素的名称(因为当你改变用户界面语言时它不会改变)Dim 响应为整数选择案例 LCase(Trim(PropertyName))案例摘要"setCoverPageProps 位置、抽象"、抽象"、wdContentControlText案例类别"setMSCoreProps 位置、类别"、类别"、wdContentControlText案例公司"setExtendedProps Location, "Company", "Company", wdContentControlText案例内容状态"setMSCoreProps 位置、contentStatus"、Status"、wdContentControlText案例创造者"setDCoreProps 位置、创建者"、作者"、wdContentControlText案例公司地址"setCoverPageProps Location, "CompanyAddress", "Company Address", wdContentControlText案例公司邮箱"setCoverPageProps Location, "CompanyEmail", "Company E-mail", wdContentControlText案例公司传真"setCoverPageProps Location, "CompanyFax", "Company Fax", wdContentControlText案例公司电话"setCoverPageProps Location, "CompanyPhone", "Company Phone", wdContentControlText案例说明"setDCoreProps 位置、描述"、评论"、wdContentControlText案例关键词"setMSCoreProps 位置、关键字"、关键字"、wdContentControlText案例经理"setExtendedProps Location, "Manager", "Manager", wdContentControlText案例发布日期"setCoverPageProps Location, PublishDate", Publish Date", wdContentControlDate案例主题"setDCoreProps 位置、主题"、主题"、wdContentControlText案例标题"setDCoreProps 位置、标题"、标题"、wdContentControlText案例pbp-projectcode"setSharepointProps Location、ProjectName"、PBP-ProjectCode"、wdContentControlComboBox案例ectd-title"setSharepointProps 位置、eCTD_x002d_Title"、eCTD-Title"、wdContentControlComboBox案例ectd-regulator"setSharepointProps Location、Regulator"、eCTD-Regulator"、wdContentControlComboBox案例ectd-subtype"setSharepointProps Location、SubmissionType"、eCTD-SubType"、wdContentControlComboBox案例ectd-subseq"setSharepointProps 位置、eCTD_x002d_SubmissionSequence"、eCTD-SubSeq"、wdContentControlComboBox案例ectd-modulelabel"setSharepointProps 位置、eCTD_x002d_ModuleName"、eCTD-ModuleLabel"、wdContentControlComboBox案例ectd-sectionlabel"setSharepointProps 位置、SectionTitle"、eCTD-SectionLabel"、wdContentControlComboBox案例ectd-subsectionindex"setSharepointProps 位置、eCTD_x002d_SubSection_x0023_"、eCTD-SubSectionIndex"、wdContentControlComboBox案例ectd-subsectionlabel"setSharepointProps 位置、e_x002d_CTD_x002d_SubsectionLabel"、eCTD-SubsectionLabel"、wdContentControlComboBox其他情况response = MsgBox("无法识别的属性名称:" & PropertyName, _vbCritical,插入文档属性")结束选择结束子Sub setCoverPageProps(Location, PropertyName, TitlePlaceHolder, ContentType)'常量缺失 = 无const coverPageMappings = "xmlns:ns0='http://schemas.microsoft.com/office/2006/coverPageProps'";使用 Location.ContentControls.Add(ContentType).Title = TitlePlaceHolder.XMLMapping.SetMapping "/ns0:CoverPageProperties[1]/ns0:"&物业名称 &[1]",coverPageMappings,没有.SetPlaceholderText 缺失,缺失,[";&TitlePlaceHolder &]".range.Select结束于结束子子 setSharepointProps(Location, PropertyName, TitlePlaceHolder, ContentType)'常量缺失 = 无'这是对应的属性:w:prefixMappingsconst sharePointPropsMappings = "xmlns:ns0='http://schemas.microsoft.com/office/2006/metadata/properties' xmlns:ns1='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns2='http://schemas.microsoft.com/office/infopath/2007/PartnerControls' xmlns:ns3='856dd977-5561-4031-9d6b-b2809bca48df'"使用 Location.ContentControls.Add(ContentType).Title = TitlePlaceHolder'这部分摘自 w:xpath=.XMLMapping.SetMapping "/ns0:properties[1]/documentManagement[1]/ns3:"&物业名称 &[1]",sharePointPropsMappings,没有.SetPlaceholderText 无,无,[";&TitlePlaceHolder &]".range.Select结束于结束子Sub setDCoreProps(Location, PropertyName, TitlePlaceHolder, ContentType)'常量缺失 = 无const DCoreMappings = "xmlns:ns0='http://purl.org/dc/elements/1.1/' xmlns:ns1='http://schemas.openxmlformats.org/package/2006/metadata/core-properties'"使用 Location.ContentControls.Add(ContentType).Title = TitlePlaceHolder.XMLMapping.SetMapping/ns1:coreProperties[1]/ns0:"&物业名称 &[1]",DCoreMappings,没有.SetPlaceholderText 无,无,[";&TitlePlaceHolder &]".range.Select结束于结束子子集MSCoreProps(Location, PropertyName, TitlePlaceHolder, ContentType)'常量缺失 = 无const MSCoreMappings = "xmlns:ns0='http://schemas.openxmlformats.org/package/2006/metadata/core-properties'";使用 Location.ContentControls.Add(ContentType).Title = TitlePlaceHolder.XMLMapping.SetMapping/ns0:coreProperties[1]/ns0:"&物业名称 &[1]",MSCoreMappings,没有.SetPlaceholderText 无,无,[";&TitlePlaceHolder &]".range.Select结束于结束子Sub setExtendedProps(Location, PropertyName, TitlePlaceHolder, ContentType)'常量缺失 = 无const extendedMappings = "xmlns:ns0='http://schemas.openxmlformats.org/officeDocument/2006/extended-properties'";使用 Location.ContentControls.Add(ContentType).Title = TitlePlaceHolder.XMLMapping.SetMapping "/ns0:Properties[1]/ns0:";&物业名称 &[1]",扩展映射,无.SetPlaceholderText 无,无,[";&TitlePlaceHolder &]".range.Select结束于结束子子测试()insertAndMapProperty 选择,eCTD-ModuleLabel";结束子

I recently found myself unable to add Control Content objects in Word that are linked to the document properties linked to the SharePoint library which are exposed when creating new columns inside a SharePoint library.

I posted my initial question: MS Word adding Custom Document Properties (from SharePoint) as a ContentControl via VBA. Unfortunately, I cannot post my solution there as some people decided that my question was incomplete.

Luckily a user (@slightlysnarky) kindly posted a solution to address part of my question How can I replicate programmatically in VBS what Word does when I insert a "built-in" property from the Insert->QuickPart->Document Property dropdown?.

解决方案

Here is what worked for me to link content control to the Document Properties inherited from the SharePoint library (an example is given for user-defined columns).

From the solution provided by @slightlysnarky, I was unclear how to find the .XMLMapping.SetMapping path to the document properties.

In order to find this information, I did the following:

  1. Create an MS Word file in my SharePoint library (with some user columns)
  2. Insert in the word file the control content manually (see original question)
  3. Save the file
  4. Change word file extension to .zip
  5. Extract [documentName.docx.zip]\word\document.xml document contained in the archive
  6. Open the file with my favorite XML editor
  7. You can then find the general path of the document at the following element\attributes: <w:dataBinding w:prefixMappings="[rootOfProperties]" w:xpath="[pathToProperties]" ....>
    • In my case I had: w:prefixMappings=""xmlns:ns0='http://schemas.microsoft.com/office/2006/metadata/properties' xmlns:ns1='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns2='http://schemas.microsoft.com/office/infopath/2007/PartnerControls' xmlns:ns3='856dd977-5561-4031-9d6b-b2809bca48df'"
    • The w:prefixMappings attributes were identical for all properties. It might change for different libraries (to be verified)
    • The w:xpath attributes were different for each property and match the information I could find the document "XML Map" (see the original question for screenshot).
    • I realized as well that unfortunately once a column is created in SharePoint, the name will not change in the XML mapping regardless of whether the name is changed in SharePoint or not, which could be expected. Hence, you can see in the code below that I have in some cases properties which mapping differs from the desired given name in the final SharePoint. Lesson learned is that it is best to carefully think of naming convention before creating the library columns.
  8. With this information, I modified the code provided by @slightlysnarky as a VBA macro inside my normal template and everything worked.

For the sake of helping others, here is my code adapted from @slightlysnarky. Note that you will need to adjust based on your library setting as explained above. * Changes go into: the Sub setSharepointProps() * You will need to change: sharePointPropsMappings=[rootOfProperties] and .XMLMapping.SetMapping [pathToProperties] as extracted from document.xml (see above)


' a simple test -  place inside the normal .dotm file VBA content
' or wherever you want the code to reside.
' for a quick test run the test() sub. It will instert a mapped control
' content in your document

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)

    Dim response As Integer
    
    Select Case LCase(Trim(PropertyName))
    Case "abstract"
        setCoverPageProps Location, "Abstract", "Abstract", wdContentControlText
    Case "category"
        setMSCoreProps Location, "category", "Category", wdContentControlText
    Case "company"
        setExtendedProps Location, "Company", "Company", wdContentControlText
    Case "contentstatus"
        setMSCoreProps Location, "contentStatus", "Status", wdContentControlText
    Case "creator"
        setDCoreProps Location, "creator", "Author", wdContentControlText
    Case "companyaddress"
        setCoverPageProps Location, "CompanyAddress", "Company Address", wdContentControlText
    Case "companyemail"
        setCoverPageProps Location, "CompanyEmail", "Company E-mail", wdContentControlText
    Case "companyfax"
        setCoverPageProps Location, "CompanyFax", "Company Fax", wdContentControlText
    Case "companyphone"
        setCoverPageProps Location, "CompanyPhone", "Company Phone", wdContentControlText
    Case "description"
        setDCoreProps Location, "description", "Comments", wdContentControlText
    Case "keywords"
        setMSCoreProps Location, "keywords", "Keywords", wdContentControlText
    Case "manager"
        setExtendedProps Location, "Manager", "Manager", wdContentControlText
    Case "publishdate"
        setCoverPageProps Location, "PublishDate", "Publish Date", wdContentControlDate
    Case "subject"
        setDCoreProps Location, "subject", "Subject", wdContentControlText
    Case "title"
        setDCoreProps Location, "title", "Title", wdContentControlText
    Case "pbp-projectcode"
        setSharepointProps Location, "ProjectName", "PBP-ProjectCode", wdContentControlComboBox
    Case "ectd-title"
        setSharepointProps Location, "eCTD_x002d_Title", "eCTD-Title", wdContentControlComboBox
    Case "ectd-regulator"
        setSharepointProps Location, "Regulator", "eCTD-Regulator", wdContentControlComboBox
    Case "ectd-subtype"
        setSharepointProps Location, "SubmissionType", "eCTD-SubType", wdContentControlComboBox
    Case "ectd-subseq"
        setSharepointProps Location, "eCTD_x002d_SubmissionSequence", "eCTD-SubSeq", wdContentControlComboBox
    Case "ectd-modulelabel"
        setSharepointProps Location, "eCTD_x002d_ModuleName", "eCTD-ModuleLabel", wdContentControlComboBox
    Case "ectd-sectionlabel"
        setSharepointProps Location, "SectionTitle", "eCTD-SectionLabel", wdContentControlComboBox
    Case "ectd-subsectionindex"
        setSharepointProps Location, "eCTD_x002d_SubSection_x0023_", "eCTD-SubSectionIndex", wdContentControlComboBox
    Case "ectd-subsectionlabel"
        setSharepointProps Location, "e_x002d_CTD_x002d_SubsectionLabel", "eCTD-SubsectionLabel", wdContentControlComboBox
    Case Else
        response = MsgBox("Unrecognized property name: " & PropertyName, _
                vbCritical, "Insert Document Properties")
    End Select

End Sub

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

Sub setSharepointProps(Location, PropertyName, TitlePlaceHolder, ContentType)
    'Const missing = Nothing
    'THis is the property corresponding to: w:prefixMappings
    Const sharePointPropsMappings = "xmlns:ns0='http://schemas.microsoft.com/office/2006/metadata/properties' xmlns:ns1='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns2='http://schemas.microsoft.com/office/infopath/2007/PartnerControls' xmlns:ns3='856dd977-5561-4031-9d6b-b2809bca48df'"
    With Location.ContentControls.Add(ContentType)
      .Title = TitlePlaceHolder

      'This part is extracted from w:xpath=
      .XMLMapping.SetMapping "/ns0:properties[1]/documentManagement[1]/ns3:" & PropertyName & "[1]", sharePointPropsMappings, Nothing
      .SetPlaceholderText Nothing, Nothing, "[" & TitlePlaceHolder & "]"
      .range.Select
    End With
End Sub

Sub setDCoreProps(Location, PropertyName, TitlePlaceHolder, ContentType)
    '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(ContentType)
      .Title = TitlePlaceHolder
      .XMLMapping.SetMapping "/ns1:coreProperties[1]/ns0:" & PropertyName & "[1]", DCoreMappings, Nothing
      .SetPlaceholderText Nothing, Nothing, "[" & TitlePlaceHolder & "]"
      .range.Select
    End With
End Sub

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

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

Sub test()
    insertAndMapProperty Selection, "eCTD-ModuleLabel"
End Sub


这篇关于通过 SharePoint 中的 VBA 自定义文档属性添加 MS Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆