0
votes

We have a 3rd party VBA module which we are tweaking to suit our needs. What we are trying to do is to search for a file path in the footer of a Word document, and replace the file path with a (3rd party generated) document ID.

The code is actually currently very straightforward and almost works. We have no problems overwriting the file path in the document footer with the required document ID, however currently our code is overwriting all text in the Word document footer (page number, initials of the person who wrote the document etc.) We just want the file path replaced.

Can anyone suggest where we're going wrong with the code below? The code currently searches for an existing ID; if it can't find that, but finds a file path it tries to overwrite; otherwise it writes the document ID to the document from scratch:

Set tSearch = doc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Find
tSearch.Text = idStr
If Not tSearch.Execute() Then
    'Current ID wasn't found; look for and replace an older one.
    tSearch.MatchWildcards = True
    tSearch.Text = "?:\*"
    If Not tSearch.Execute(ReplaceWith:=idStr) Then
        doc.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = False
        doc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = idStr
        doc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Font.Size = 8
    End If
End If
1
Are you still looking for a solution?Siddharth Rout

1 Answers

0
votes

For starters, the search will probably always fail. The find text needs to be more like:

tSearch.Text = "?:\\* "

The backslash is a control character and needs to marked with a literal character, which by chance is the backslash. Also the * isn't a true "find everything" symbol but think of it as "find everything until." In this case, a space character but it could be a period, end of line, or so on.