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!

Word: Finding text in the footer with .Find method 1

Status
Not open for further replies.

Sashanan2

Programmer
Jul 2, 2004
39
NL
I'm having a VB6 application find (and subsequently replace) certain strings in a Word template, and I've now run into trouble when I'm trying to replace string in the footer. Evidently, the method I'm using only works for text in the main document and skips over the footer.

Doing it as follows:

With wrdWordApplication.Selection.Find
.ClearFormatting
.Text = strMyString
.Execute Forward:=True, Wrap:=Word.WdFindWrap.wdFindContinue
End With

Works fine to find any string in the main text but it never seems to search the footer. Oddly, if I record a macro in Word and do a find then (which presumably should get me the code equivalent to use), it does find the text in the footer, then when I review the code the macro recorder produce, it *is* the above.

I've been wandering around old topics here and on MDSN for most of the day trying to find alternative solutions but nothing seems to work so far. Might just be missing something obvious what with the heatwave we're having, and this being my first serious VB-to-Word work. Can anybody help me out here?

"Any fool can defend his or her mistakes; and most fools do." -- Dale Carnegie
 
Have a look in the VBA help file at NextStoryRange

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The solution at NextStoryRange didn't quite do what I wanted, but it did put me on the right track and made me come up with:

Code:
Public Sub FillInFooter(SearchText As String, OutputText As String, _
WordSession As Word.Application)

Dim myStoryRange As Range
For Each myStoryRange In WordSession.ActiveDocument.StoryRanges
    If myStoryRange.StoryType = wdPrimaryFooterStory Then
        myStoryRange.Find.Execute FindText:=SearchText, Forward:=True
        myStoryRange.Text = OutputText
    End If
Next myStoryRange

End Sub

which did the trick. Thanks for the help!

"Any fool can defend his or her mistakes; and most fools do." -- Dale Carnegie
 
This will work if you never have any footers set as Different first page, or Different Odd/Even. Your code only search wdHeaderFooterPrimary.

Gerry
See my Paintings and Sculpture
 
Accomodating for the scenarios you mention would be a simple matter of just expanding the "If myStoryRange.StoryType = wdPrimaryFooterStory" part to include all possible footer options, right?

"Any fool can defend his or her mistakes; and most fools do." -- Dale Carnegie
 
Yes. The other two are: wdheaderFooterFirstPage, and wdheaderFooterEvenPages.

Note that there is NO odd page footer. Primary is the footer (or header) for ALL pages if Different first page, and Different Odd/Even are not set to TRUE.

Primary is the footer for Odd and Even pages if Different first page = True, and Different Odd/Even = False;

Primary is Odd pages if Different fist page and Different ODD/Even are both = True.

Note also that Different first page, and Different Odd/Even are given values IN SEQUENCE to the value for Primary. They are not NULL. If they are set and then, say Different first page, is then set to False, you can get some odd returns. Such as:
Code:
If ActiveDocument.Sections(2) & _
   .Footers(wdHeaderFooterFirstPage).Exists = True Then
   MsgBox "True"
Else
   MsgBox "False" & ActiveDocument.Sections(2) & _
      .Footers(wdHeaderFooterFirstPage).Range.Text
End If

which states that Footers(wdHeaderFootersFirstPage does NOT exist....but displays its text value anyway!

For this reason, always be careful using the .Exists property of header/footers. The .Exist property applies to Options object, and only indicates if the property is set to True. It can be set to False, but it can, and does, contain a value.

Gerry
sorry...my art web site is down
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top