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

Word Find function not highlighting text

Status
Not open for further replies.

bodmin

Technical User
Apr 1, 2004
98
GB
Hi guys,

I have got a piece of macro code that uses the find function to search through a word document to locate a particular instance of a font with certain formatting.

The code works fine on documents without textboxes but on any document that contains textboxes and multiple stories the find function is reporting that .Found = True but it is then not highlighting anything that it has found. I have posted the find code I am using below. Anyone got any ideas??

For Each oStory In ActiveDocument.StoryRanges
With ActiveDocument.Content.Find
.ClearFormatting
.Font.Name = strFontToFind

.Font.Size = strFontSizeToFind

.Font.Bold = FontBoldIndicator

.Font.Italic = FontItalicIndicator

With .Replacement
.ClearFormatting
.Highlight = True
End With
Search_Font = ActiveDocument.Content.Find.Font.Name
Search_Size = ActiveDocument.Content.Find.Font.Size
MsgBox ("Search Font = " & Search_Font)
MsgBox ("Search Size = " & Search_Size)


'.Text = ""

'.Replacement.Text = ""

'.Forward = True

'.Wrap = wdFindContinue

'.Format = True

'.MatchCase = False

'.MatchWholeWord = False

'.MatchWildcards = False

'.MatchSoundsLike = False

'.MatchAllWordForms = False

'.Highlight = True

.Execute FindText:="", ReplaceWith:="", Format:=True

If .Found = True Then
MsgBox "The font has been found and is highlighted in yellow, please remember to use the clear search macro before closing the document to clear the highlighting"
Else
MsgBox "The font could not be found, if you are sure that this font is in the document please retry the search and make sure that you have selected the intended font format details"
End If

End With
Next
 
which version of word are you using ?

Chance,

Filmmaker, gentleman and ROMAN!
 
Code:
For Each oStory In ActiveDocument.StoryRanges
    With ActiveDocument.Content.Find

the Activedocument.content.find is independant of the storyranges

try

Code:
For Each ostory In ActiveDocument.StoryRanges

    Set myrange = ActiveDocument.StoryRanges(ostory.Name).Content
    
    myrange.Find ....



Chance,

Filmmaker, gentleman and ROMAN!
 
Hi chance,

thanks for the help so far, has got me a stage further, had to make some slight modifications to the code you suggested which is as follows:

Set myrange = oStory
myrange.WholeStory

With myrange.Find....

The code is now successfully highlighting the first sentence where the particular character occurs in the last story to be searched rather than highlighting all occurrences of that character in all stories.

Can you think of why it is doing this and also a way to select every occurence?

many thanks in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top