如何使用 VBA 更新 link 到内容的 MS Word 自定义文档属性?

How do you update MS Word Custom Document Properties that link-to-content using VBA?

我的文档有一个带有书签内容控件的表单,通过 Link-to-Content 选项 link 自定义文档属性。自定义文档属性用于在文档后面的报告中显示表单值。该方法的唯一问题是,当表单中的值更新时,它不会自动更新 linked 自定义文档 属性.

的值

手动更新表单时,我发现只要访问“高级属性”对话框就会更新 link。 (然后我 select 报告内容并按 F9 更新 DOCPROPERTY 字段。)但是现在我需要自动填写表格,我找不到更新 link 的方法 VBA。我尝试访问自定义文档属性,甚至用原始源替换 link 源,认为访问 links 可能会更新它们(参见下面的代码),但是 link ed 值不会以这种方式更改。有谁知道如何以编程方式更新那些 links? “高级属性”对话框是如何做到的?

With wdDoc
    For Each dp In .CustomDocumentProperties
        If dp.LinkToContent Then
            dp.LinkSource = dp.LinkSource
        End If
    Next dp
    .Content.Fields.Update    
End With
 

我发现通过将选项设置为“打开时更新链接”,链接会在我保存文档时更新。所以保存后,我可以更新字段,然后再次保存文档以实现我想要的。

With wdApp
    'updates OLE links to Content Controls
    .Options.UpdateLinksAtOpen = True
End With
...
With wdDoc
    .SaveAs sFileName
    .Content.Fields.Update
    For i = 1 To .Sections.Count
        .Sections(i).Headers(wdHeaderFooterPrimary).Range.Fields.Update
        .Sections(i).Footers(wdHeaderFooterPrimary).Range.Fields.Update
        .Sections(i).Headers(wdHeaderFooterFirstPage).Range.Fields.Update
        .Sections(i).Footers(wdHeaderFooterFirstPage).Range.Fields.Update
    Next i
    .Save sFileName
End With

我将继续研究自定义 XML 部件,但作为长期解决方案。看起来这就是微软的方向,最终对二进制自定义文档属性的支持可能会消失。感谢您引导我朝那个方向前进。