【问题标题】:Insert Document Property Object插入文档属性对象
【发布时间】:2022-03-19 16:49:52
【问题描述】:

在我的 Word (Office 365) 文档中,我可以通过单击插入/快速部件/文档属性/公司来插入公司名称的快速部件。

我想要一个宏来做到这一点,所以我可以在我的快速访问工具栏上弹出一个按钮,让它一键点击而不是四键。

当我记录这个过程时,宏没有注册插入。我发现下面的 VBA 代码插入了字段的当前文本,而不是内容控件本身:

ActiveDocument.Content.InsertAfter
ActiveDocument.BuiltInDocumentProperties(wdPropertyCompany)

我认为必须有一行 VBA 将“公司快速部件”字段插入到我的文档中,就好像我完成了那四次单击一样。

【问题讨论】:

    标签: vba ms-word word-contentcontrol


    【解决方案1】:

    诀窍是将内容控件映射到公司

    Sub insertCompanyCC()
    
    On Error GoTo err_insert
    
    Dim cc As ContentControl
    Set cc = ActiveDocument.ContentControls.Add(wdContentControlText, Selection)
    With cc
       .Title = "Company"
       .XMLMapping.SetMapping "/ns0:Properties[1]/ns0:Company[1]"
    End With
    
    exit_insert:
        Exit Sub
    
    err_insert:
        Select Case Err
            Case 4605
                 MsgBox "Please move your cursor outside of the content control.", vbExclamation
            Case Else
                Err.Raise Err.Number, Err.Source
        End Select
        Resume exit_insert
    End Sub
    

    例如/ns1:coreProperties[1]/ns0:creator[1] 将插入作者。

    【讨论】:

    • 哇,这比我预期的要复杂一些,但它确实有效,所以非常感谢你,Ike。作为一个小问题,内容控件似乎没有名称“公司” - 添加这件事容易吗?
    • 将标题属性添加到代码中
    猜你喜欢
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2013-12-05
    • 2021-09-01
    相关资源
    最近更新 更多