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!

Problems with Find Highlight Function 1

Status
Not open for further replies.

bodmin

Technical User
Apr 1, 2004
98
GB
Hi guys,

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

The code is going through each story correctly and is finding the font specified on the search form but only highlights the sentence at the top of the document that uses the specific font, even though this font is used in several other places within the same story and in other stories. Has anyone got any ideas why this is not highlighting every occurrence only the first/last found? Or any ideas as to how I can fix it?

The code I am using is below.

Many thanks in advance.
For Each oStory In ActiveDocument.StoryRanges

Set myrange = oStory
myrange.WholeStory

With myrange.Find
'.ClearFormatting
.Font.Name = strFontToFind

.Font.Size = strFontSizeToFind

.Font.Bold = FontBoldIndicator

.Font.Italic = FontItalicIndicator

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

.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
 
Could it be that you need to envoke the find next option until you meet the end of the story?

Everybody is somebodys Nutter.
 
Hi Chris,

Thanks for the help, can you give me any pointers on how to envoke the find next option?

Thanks
 
bodmin,

Have you tryed recording a macro?
This may give you some pointers.

Everybody is somebodys Nutter.
 
Have you tried this ?
.Execute FindText:="", ReplaceWith:="", Format:=True[!], Replace:=wdReplaceAll[/!]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi guys,

Thanks for the help so far, PHV the change to the execute line is now successfully highlighting all the text in the stories.

My only issue now is that this has proven to me that word is not actually counting all stories in the document, despite the loop dealing with each story in the collection for ActiveDocument. For example I have a document with 5 textboxes in, that I had assumed were being treated as separate stories. Though when I do a count on the stories within activedocument I get a result of 2 returned. This is meaning that not all occurrences of the font within the document are not being highlighted.

Can anyone think of why this would be happening or how to correct this?

many thanks in advance
 
Have a look at NextStoryRange

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi guys, thanks a lot for the help I now seem to have got everything working correctly.

As well as using NextStoryRange that you suggest PHV, have a star. Also I had to make the code check the type of story that it was processing as the NextStoryRange is not available on a story of type main and I was processing documents that contain a mixture of main stories and text stories.

The code I ended up with is as below in case anyone is interested.

For Each oStory In ActiveDocument.StoryRanges
Story_Type = oStory.StoryType

Select Case Story_Type
Case wdMainTextStory
Main_Story = True
Case wdFootnotesStory
Main_Footnotes = True
Case wdEndnotesStory
Main_End = True
Case wdCommentsStory
Comments_Story = True
Case wdTextFrameStory
Text_Story = True
Case wdEvenPagesHeaderStory
EvenPage_Header = True
Case wdPrimaryHeaderStory
Primary_Header = True
Case wdEvenPagesFooterStory
EvenPage_Footer = True
Case wdPrimaryFooterStory
Primary_Footer = True
Case wdFirstPageHeaderStory
FirstPage_Header = True
Case wdFirstPageFooterStory
FirstPage_Footer = True
End Select
Next

If Main_Story = True Or Main_Footnotes = True Or Main_End = True Or Comments_Story = True Then
If Text_Story = True Or EvenPage_Header = True Or Primary_Header = True Or EvenPage_Footer = True Or Primary_Footer = True Or FirstPage_Header = True Or FirstPage_Footer = True Then
For Each oStory In ActiveDocument.StoryRanges
NumCharacters = 0
NumCharacters = oStory.Characters.Count
If oStory.StoryType = wdMainTextStory Or oStory.StoryType = wdFootnotesStory Or oStory.StoryType = wdEndnotesStory Or oStory.StoryType = wdCommentsStory Then
Search_Characters_For_Fonts
Else
Search_Characters_For_Fonts
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
NumCharacters = 0
NumCharacters = oStory.Characters.Count
Search_Characters_For_Fonts
Wend
End If
Next
Else
NumCharacters = 0
NumCharacters = ActiveDocument.Content.Characters.Count
Search_Characters_For_Fonts
End If
Else
For Each oStory In ActiveDocument.StoryRanges
NumCharacters = 0
NumCharacters = oStory.Characters.Count
Search_Characters_For_Fonts
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
NumCharacters = 0
NumCharacters = oStory.Characters.Count
Search_Characters_For_Fonts
Wend
Next
End If
 
All that to search through those d&!@ textboxes. Man I hate those things. I admire your persistence. Thanks for posting your solution.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top