0
votes

There are excellent macros to update all fields in Microsoft Word documents (including headers and footers). Just like this one: Macro to update all fields in a word document

But what if headers and footers contain text boxes with fields? Sure, it would be better to not have any textbox there, but what if there is no other solution?

For instance, it could be applied to a problem like that:

We should make a text document template with standard form like this one: GOST standard form for text documents Every sheet contains a frame with a bottom table and a left-side table with some fields, like a document number, revisions, etc.

As for me, the better way to do it is to use references to Custom Document Properties in fields placed in a footer. But there should be tables which take a lot of space shrinking the main text area. To prevent this, we could move these tables into a large text box on a different layer. The only problem is how to update fields in this text box...

It could be achieved following these steps:

Get Active Document
Iterate through Footers in Active Document
  Iterate through Shapes in Footers in Active Document
    If the Shape has Text then
      Update Fields in the Shape
    End IF
  Next
Next

So, the way to the object is: ActiveDocument -> Footer -> TextBox -> Field

The problem is how to go there. Microsoft Word-VB reference says nothing about the situations like that.

Test document: Test MS-Word document with fields in a footer

Document has 4 custom properties to change:

  • Document Number
  • Document Name
  • Product Type
  • Parent Number
1
The code in the link does already check text boxes. Text boxes belong to the Shapes collection, so For Each oShp In rngStory.ShapeRange covers it.Cindy Meister
Thank you a lot for the answers, but this macro doesn't work for my problem. I attached a link to the test document.Alexander Semenov

1 Answers

0
votes

You might try:

Sub UpdateFields()
Application.ScreenUpdating = False
With ActiveDocument
  .Fields.Update
  .PrintPreview
  .ClosePrintPreview
End With
Application.ScreenUpdating = True
End Sub

As for the textboxes, you can replace them with single-cell tables, with wrapping if necessary. Unlike textboxes, the content even of wrapped tables remains associated with the underlying text layer.