Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace text in textbox within a footer?

Status
Not open for further replies.

clhare

Technical User
May 29, 2003
118
US
Can anyone tell me how to replace text that is in a textbox in a footer using a macro? I can replace text in a footer using a macro, but it won't replace the text if it's in a textbox in the footer.

I'm totally stumped!!

Any help is GREATLY appreciated!
 
All I can say is...why are you using a textbox in the footer?????

Nevertheless, have you tried recording a macro? It works. I had it find a graphic (in a textbox) in the main stroy, copy it, go into header/footer, find a graphic in a textbox (in the footer story), delete it, then paste the one I got from the main doc.

However, the botton line is, no you can NOT edit the existing textbox contents. While you can select it, you can not get the insertion point into the text itself. But you could delete in and replace it with another one.

An alternative, if it is really a graphic you want in the footer, is to use InLineShapes to put a graphic file.

Selection.InlineShapes.AddOLEObject ClassType:="Paint.Picture", FileName:= _
"D:\whatever\blah.bmp", LinkToFile:=False, DisplayAsIcon:=False.

If you put DisplayAsIcon as True, double clicking the icon with open your default image viewer for the filetype.

What are you doing this for again?

Gerry
 
I'm not sure why the document I'm working with has a textbox in the footer, but unfortunately I'm stuck with it.

I've been running a bunch of searches for info on how to replace text in a textbox within a footer and did find some code in Microsoft Knowledge Base at Using their code as a starting point, I modified it as follows and found that it did work without having to replace the text box itself. Here's the code I ended up with:

Code:
Dim sSection As Section
Dim hFooter As HeaderFooter
Dim shpShape As Shape

' Loop through each section in the document
For Each sSection In ActiveDocument.Sections
    ' Loop through each footer in each section
    For Each hFooter In sSection.Footers
    ' If the footer is the Primary footer
    If hFooter.Index = wdHeaderFooterPrimary Then
        ' Loop through each Shape object in the header
        For Each shpShape In hFooter.Shapes
            ' If the shapetype is a text box
            If shpShape.Type = msoTextBox Then
                With shpShape.TextFrame.TextRange.Find
                    .ClearFormatting
                    .Replacement.ClearFormatting
                    .Style = ActiveDocument.Styles("Style A")
                    .Replacement.Style = ActiveDocument.Styles("Style B")
                    .Execute findtext:="My text", _
                    Wrap:=wdFindContinue, replacewith:="My text", _
                    replace:=wdReplaceAll
                End With
            End If
        Next
    End If
    Next
Next

Thanks for your help!
 
Cool. I stand corrected - which I always like to do, as that means I have learned something. I will put that one into my can of useful things. Thanks.





Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top