0
votes

I have requirement to maintain a standard footer in Word documents. The footer contains doc id and version, which can change, so I must be able not only insert this standard footer, but also update it.

I need to identify just the standard string part of the footer and update it. The string looks like <<Libray:DocId:vX>> (the angle brackets are to help match the beginning and end of the standard footer).

A concern is that the brackets alone aren't enough to reliably identify the standard string, so I am doing a RegEx ("<<[A-Za-z0-9_-]+:\d+v\d+>>") match and Replace to achieve the update, but it seems modifying the Text property wipes out any formatting and active content.

If I can't use RegEx.Replace on the Text property w/o losing formatting, is there another way to tag and update just the standard string?

If oRegEx.Test(strFooter) Then
        ' Set the footer for the active document
        With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
            .Style = wdStyleFooter
            .Font.Size = 8
            .Text = oRegEx.Replace(strFooter, strTextString) 'this wipes out page numbering and formatting
        End With
    Else
        With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
            .Style = wdStyleFooter
            .Font.Size = 8
            .InsertAfter strJoiner & strTextString 'this preserves formatting and active content
        End With
    End If
1
I do not have much experience with Word but I have you tried Fields? I was reading about it and seems like a wayuser5412293
If you stick to using Range and .InsertAfter your formatting should stay as it is. I just altered a footer sprinkled with formattings like this and it worked just fine: ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.InsertAfter " yadda!!".LocEngineer

1 Answers

2
votes

The problem you're running into is that oRegEx.Replace will work only with text content. It has no knowledge of and cannot retain any formatting that might be present in strFooter. What the first block of code is essentially doing is replacing the entire content of the Footer with the result of the RegEx.

InsertAfter, on the otherhand, leaves the original Range's content intact. There are other, similar approaches - which is appropriate depends on exactly what you want to do.

Since you don't detail why you're using the RegEx, although you mention "find", it's difficult to give provide additional helpful information. Word does have its own Find/Replace functionality, as a property of the Range object. Word's Find does support Wildcards, which is similar to RegEx.

EDIT: thank you for the additional info. In Word, switch to the header/footer view. Press Ctrl+F to display the Find functionality. If this opens a task pane, click on the arrow at the right of the drop-down and choose "Advanced find" to get the full dialog box. Click the "Replace" tab.

Activate the "wildcards" checkbox. In the "Find" box enter a search code similar to the following, but substitute valid text for the document you're testing on: (\<\<)Library:DocId:vs#(>>)

In the Replace box: \1Abc:efg:vsn2\2

The parentheses Find define "expressions" that can be referenced in Replace so that this content can be retained. The result of the above should be <>.

Once you have this working, record executing the Find/Replace steps in a macro. That will give you the syntax to use in your code.

An additional tip for your code: If the entire footer should have the different font size you should change the Footer style definition, rather than adding it as direct formatting.