PDA

View Full Version : Replacing text in footer



gav12345
05-29-2012, 12:58 AM
Hi all,

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

Thanks, Gavin

macropod
06-01-2012, 05:48 AM
Hi Gavin,

Try:
Dim Scn As Section
Set Scn = doc.Sections(1)
With Scn.Footers(wdHeaderFooterPrimary).Range
With .Find
.Text = "[A-Z]:\\*.do[cxm]{1,2}"
.MatchWildcards = True
.Execute
End With
If .Find.Found Then
With .Duplicate
.Text = idStr
.Font.Size = 8
End With
Scn.PageSetup.DifferentFirstPageHeaderFooter = False
End If
End With
Set Scn = Nothing

gav12345
06-11-2012, 01:35 AM
Thanks Paul, I'll give that a try later today (sorry for delay in replying, just back from holiday).
Gavin

gav12345
06-18-2012, 01:39 AM
Many thanks Paul, that works perfectly.