clhare
02-16-2010, 01:06 PM
I have a Word file that has footers which contain text similar to the following. Hopefully, this makes sense. There are a couple of returns, then on the text line, there's a tab and the text is right-aligned.
Original Footer Text:
[return]
[return]
[tab] 12345 Text Abc1[return]
In the text of the footer, there is some text that is always the same (example: "12345 Text") which is followed by text that varies from file to file (example: "Abc1", or (Bcd 225"). I need to replace everything shown in red in the above example--starting with "12345 text" through the end of the line, without including the tab at the start of the line or the paragraph mark at the end of the line.
Since the last part of the text varies in length, I need to search for just the first part ("12345 Text"), then extend the range to the end of the line (without including the paragraph mark). I've almost got it to work, but not quite. My code below does not appear to be extending the range to the end of the line. When I run the macro, the variable text at the end of original footer text ("Abc1") ends up at the beginning of the updated footer text as shown in red in the following example:
Updated Footer Text:
[return]
[return]
[tab] Abc1678910 Newtext[return]
How Footer Text Should Appear After Update:
[return]
[return]
[tab] 678910 Newtext[return]
Can someone help me figure out how to fix my code? Here's what I have so far:
Dim HF As HeaderFooter
Dim i As Integer
Dim intHFType As Integer
Dim rngPane As Range
With ActiveDocument.Sections(1)
For intHFType = 1 To 3
Set rngPane = .Footers(intHFType).Range
With rngPane.Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(FindText:=" 12345 Text", _
Forward:=True) = True
With rngPane
.Text = ""
.Expand Unit:=wdParagraph
.MoveEnd Unit:=wdCharacter, Count:=-1
.InsertAfter ("678910 Newtext")
.Collapse 0
End With
Loop
End With
.Footers(intHFType).Range.Borders(wdBorderTop) _
.LineStyle = wdLineStyleNone
Next intHFType
End With
Thanks!
Original Footer Text:
[return]
[return]
[tab] 12345 Text Abc1[return]
In the text of the footer, there is some text that is always the same (example: "12345 Text") which is followed by text that varies from file to file (example: "Abc1", or (Bcd 225"). I need to replace everything shown in red in the above example--starting with "12345 text" through the end of the line, without including the tab at the start of the line or the paragraph mark at the end of the line.
Since the last part of the text varies in length, I need to search for just the first part ("12345 Text"), then extend the range to the end of the line (without including the paragraph mark). I've almost got it to work, but not quite. My code below does not appear to be extending the range to the end of the line. When I run the macro, the variable text at the end of original footer text ("Abc1") ends up at the beginning of the updated footer text as shown in red in the following example:
Updated Footer Text:
[return]
[return]
[tab] Abc1678910 Newtext[return]
How Footer Text Should Appear After Update:
[return]
[return]
[tab] 678910 Newtext[return]
Can someone help me figure out how to fix my code? Here's what I have so far:
Dim HF As HeaderFooter
Dim i As Integer
Dim intHFType As Integer
Dim rngPane As Range
With ActiveDocument.Sections(1)
For intHFType = 1 To 3
Set rngPane = .Footers(intHFType).Range
With rngPane.Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(FindText:=" 12345 Text", _
Forward:=True) = True
With rngPane
.Text = ""
.Expand Unit:=wdParagraph
.MoveEnd Unit:=wdCharacter, Count:=-1
.InsertAfter ("678910 Newtext")
.Collapse 0
End With
Loop
End With
.Footers(intHFType).Range.Borders(wdBorderTop) _
.LineStyle = wdLineStyleNone
Next intHFType
End With
Thanks!