Determine if found text is within a Table Of Contetns

M

mike100

Hi all, I'm really hoping someone can help me out here. I've spent days
searching countless forums and cannot find an answer to this one.

How would I modify the example macro below to search an entire document for a
string, EXCLUDING the Table OF Contents.

I've found out how to tell if the string is within a table (also included
below), but I haven't found a way of determining if the found text is within
a Table Of Contents.

Sub Macro1()
Dim MyRange As Object
Set MyRange = ActiveDocument.Range
MyRange.Find.Text = "find me"
MyRange.Find.Forward = True
MyRange.Find.Wrap = wdFindStop
MyRange.Find.MatchWildcards = WildCardSearch

Do While MyRange.Find.Execute = True

If MyRange.Information(wdWithInTable) = False Then 'Determines if
selection is within a table
'Do Stuff here
End If

Loop

End Sub



Any help would be gladly appreciated!

Kind Regards

Mike
 
D

Doug Robbins - Word MVP

Use the following

Dim TOC As Range
Set TOC = ActiveDocument.TablesOfContents(1).Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Boston", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=True) = True
If Selection.Start >= TOC.Start And Selection.End <= TOC.End Then
MsgBox "It's in the Table of Contents."
Else
MsgBox "It is not in the Table of Contents."
End If
Selection.Collapse wdCollapseEnd
Loop
End With

For the document on which I make use of this, the word "Boston" was in the
Table of Contents as well as in other locations in the document. When the
macro is run, the appropriate message is displayed, depending upon the
location of the found word

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top