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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find string on first page in Word 2003 1

Status
Not open for further replies.

Strobeman

Programmer
May 16, 2003
40
AU
Hi Guys,
I have a function that returns a Boolean based on whether a string is found on the first page of my document or not. It is returning False when I know it should be True.

Function SearchForSentence(strSentence As String) As Boolean

Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=1
With Selection.Find
.Text = strSentence
.Forward = True
.Wrap = False
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
SearchForSentence = Selection.Find.Execute

End Function

The string I am searching for is "FEDERAL COURT OF"

Any ideas where I have gone wrong?
Thanks
 
Hi Strobeman.

WHAT is going wrong to begin with?

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Hi Strobeman,

As coded, you function returns 'True' in my testing even if the search string is on a page other than the first. Try it this way:
Code:
Function SearchForSentence(strSentence As String) As Boolean
With Selection
  .GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=1
  .GoTo(What:=wdGoToBookmark, Name:="\page").Select
  With .Find
    .Text = strSentence
    .Forward = True
    .Wrap = False
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    SearchForSentence = .Execute
  End With
End With
End Function

Sub Test()
MsgBox SearchForSentence("FEDERAL COURT OF")
End Sub


Cheers
[MS MVP - Word]
 
Slightly shorter version.
Code:
Function SearchForSentence(strSentence As String) As Boolean
Dim r As Range
Selection.HomeKey Unit:=wdStory
Set r = ActiveDocument.Range( _
   Start:=0, _
   End:=ActiveDocument.Bookmarks("\Page").Range.End)
With r.Find
   .Text = strSentence
   SearchForSentence = .Execute
End With
End Function


Sub Test()
MsgBox SearchForSentence("FEDERAL COURT OF")
End Sub

Gerry
 
Thanks Tony. I can not see why either. If the string is there, it should return a True. I copied it over to a test document, and it worked fine.

Gerry
 
Thanks guys, I appreciate the refined code. MakeItSo, the function is just returning false every time. I will tinker a bit more with it today.
 
Ok, got it working by moving the call to the function a bit further up in my code. Thought it might have something to do with the ActiveDocument not being active but the lines before are doing a word count on the ActiveDocument without any problems. Very strange.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top