0
votes

My document has a form with bookmarked content controls that link to Custom Document Properties via the Link-to-Content option. The Custom Document Properties are used to display form values in reports later in the document. The only problem with that approach is that when a value is updated in the form, it doesn't automatically update the value of the linked custom document property.

When updating the form manually, I found just accessing the Advanced Properties dialog updates the links. (Then I select report content and press F9 to update the DOCPROPERTY fields.) But now I need to automate filling in the form, and I can't find a way to update the links in VBA. I've tried accessing the custom document properties, and even replacing the link source with the original source thinking that accessing the links might update them (see code below), but the linked values aren't changed this way. Does anyone know how to programmatically update those links? How does the Advanced Properties dialog do it?

With wdDoc
    For Each dp In .CustomDocumentProperties
        If dp.LinkToContent Then
            dp.LinkSource = dp.LinkSource
        End If
    Next dp
    .Content.Fields.Update    
End With
 
1
If you are using content controls why aren't you linking them directly to a Custom XML Part instead of using custom properties? Content controls update automatically whereas fields don't, and custom properties that are linked to content are flaky at best.Timothy Rylatt
For more on what Timothy Rylatt is talking about, see my page on duplicating data in a document using mapped content controls: addbalance.com/word/MappedControls.htm#PageStartCharles Kenyon
Thanks for responding. I didn't look much into custom XML mapping because it looked like that would require an add-in XML file that contained the mappings. My organisation doesn't permit any add-ins to their SOEs and they don't generally allow macros at all, but after some time, I managed to get consent from security to have my macro certified as 'trusted' because it would save a lot time generating reports. I'm pretty sure they won't allow any exception to the add-in rule, so I'm constrained to work within the boundaries of out-of-the-box Word.Gabrielle
FWIW, you're right that the content link is flaky, or hasn't been thought through. I tried forcing an update by unlinking the bookmark in LinkToSource from the property by updating the value, and then relinking the bookmark, but I wasn't able to change the value of either LinkToContent or LinkSource, even though the docs say they are read/write. (I can set LinkToContent to False, but the Debugger shows the value remains True, and if I try to enter "" in LinkSource, I get a runtime error.) The docs say if LinkSource has a value, LinkToContent is automatically set to True, so it's a Catch-22.Gabrielle
The custom XML part is contained within the document or template. Office documents are basically just zip archives that contain multiple XML files.Timothy Rylatt

1 Answers

0
votes

I found that by setting Options to "Update Links at Open", the links are updated when I Save the document. So after saving it, I could update the fields, and Save the document again to achieve what I wanted.

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

I will continue to investigate Custom XML Parts though as a longer term solution. It looks like that's where Microsoft's direction is, and eventually support for the binary Custom Document Properties may disappear. Thanks for steering me in that direction.