Log in

View Full Version : Need help replacing text in footers



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!

fumei
02-16-2010, 02:26 PM
1. why do you have those empty returns? Note that in the demo attached I replaced those empty returns with an explicit style - FooterText.

2. click ReplaceFooter on the top toolbar.

Code:
Sub ReplaceFooter()
Dim oHF As HeaderFooter
Set oHF = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
oHF.Range.Text = "678910 Newtext"
End Sub

Or even:

Sub ReplaceFooter()
ActiveDocument.Sections(1) _
.Footers(wdHeaderFooterPrimary).Range.Text = "678910 Newtext"
End Sub

I am pretty sure this is NOT fully what you are looking for, but then perhaps explain in better detail.

For example: why do you need to do a search, and then expand to the end of the paragraph? Why do you need a tab there, if the text is right justified? In my demo, there is no Tab.

clhare
02-17-2010, 05:01 AM
The tab is in the odd page footers. In the even page footers there is no tab and the text is left-aligned. Also, there's actually an active-x textbox that is anchored on the text line in the footers, so I can just replace the entire footer. So I am trying to just replace the specific text through the end of the line so I don't disturb anything else in the footers.

fumei
02-17-2010, 09:41 AM
What is in the activeX control? Why do you have it?

"there's actually an active-x textbox that is anchored on the text line in the footers, so I can just replace the entire footer"

How is this possible? If there is a control how can you just replace the entire footer? Does that mean you want to get rid of the control?

clhare
02-17-2010, 08:56 PM
It's used to generate a barcode. I don't know that much about it. Someone else created that piece and set it up. I deal with the rest of the template. I need it to stay in and the tab has to stay in the first page footer and odd page footer. Is it possible to select text starting with the part that is always used and then including whatever text follows it?

fumei
02-18-2010, 10:24 AM
I am not following this.

Do you, or do you not, have empty returns (paragraphs)?

Do you, or do you not, want to retain the activeX control. It seems you do. I fail to see how a ActiveX textbox generates a barcode.

I fail to see why you use tabs for odd/even. Use Styles.

Take a look at the attached - before you click the "Replace Footer".

1. There is a ActiveX textbox in both the Even footer, and the Primary footer. (Primary acts as Odd...there is NO odd footer object). IMO, as it stands, the ActiveX control is irrelevant to what you seem to want to do.

2. The text for both is "12345 Text Abc1"

3. There is NO tab. The "left" side footer uses...wait for it... "LeftFooter", and the "right" side footer uses...wait for it..."RightFooter".

Here is the full code.

Sub ReplaceFooter()
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) _
.Range.Paragraphs(2).Range.Text = "678910 Newtext"
ActiveDocument.Sections(1).Footers(wdHeaderFooterEvenPages) _
.Range.Paragraphs(2).Range.Text = "678910 Newtext"
End Sub


Again, WHY are you searching at all? If what you want to do is change:

[paragraph holding ActiveX control]
12345 Text Abc1[return] - RIGHT aligned

to

[paragraph holding ActiveX control]
678910 Newtext[return] - RIGHT aligned

Then, simply do that. Which the code above does...for both left and right (odd and even) footers.

Darn, I can not upload the new file as I have exceeded my quota of files. Hmmmmmm.

JulieC
02-23-2010, 03:01 PM
I may have changed the FindText string to make it work, but it did.


Dim j as Integer
'...

Do While .Execute(FindText:="12345 Text", Forward:=True) = True
With rngPane
j = .Characters.Count
'.Text = ""
.Expand Unit:=wdParagraph
.MoveStart Unit:=wdCharacter, Count:=j
.MoveEnd Unit:=wdCharacter, Count:=-1
'.InsertAfter ("678910 Newtext")
.Delete
.InsertAfter ("678910 Newtext")
.Collapse 0
End With
'...

fumei
02-24-2010, 11:07 AM
I say it again. I do not understand why you are doing this. I do not understand why you are doing any MoveStart etc.

I can upload now. Take a look. See that in the footers there is a ActiveX textbox (why I have no real idea, but there it is), and the starting text "12345 Text abc1".

Further, it is left aligned for one page, right aligned for the other. There is no use of a tab, as it uses a Style.

Click "Replace Footer" on the top toolbar.

It changes the text in the footer. No searching. No character counting. No MoveStart.

clhare
03-01-2010, 05:27 AM
I'm trying to find the fastest way to update the footers based on how the documents I'm updating are currently set up. So, I'm trying to deal with the styles they have and just update the text from the search string to the end of the line.

I have just started working on the updates now and am using a modified version of my original macro. In order to get rid of the variable text at the end of the line, I am using an input box and typing that part, and then the macro searches for the text that is always there plus the text I typed in the input box and replaces the whole thing with the new text.

Not the best solution, but it's getting the job done.