Checkign for TOC Fields in a document

D

DebbiePartridge

Hi All

Is there a way of identifying whether I have any TOC fields in a
document. We create tables of content using styles but sometimes use
TOC fields too. I'd like to check whether there are any in the
document prior to displaying a dialog screen.

Many thanks
Debbie
 
P

Peter Hewett

Hi Debbie

It's fairly strightforward, use the following code:

Public Function TOCEntryFields() As Boolean
Dim lngJunk As Long
Dim rngStory As Word.Range
Dim fldItem As Word.Field

' Word missing first Header/Footer bug workaround
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType

' Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges

' Iterate through all linked stories
Do
' If we find a TOCEntry field we're done
For Each fldItem In rngStory.Fields
If fldItem.Type = wdFieldTOCEntry Then
TOCEntryFields = True
Exit Do
End If
Next

' Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Function

You can call the code something like this:

If TOCEntryFields Then
MsgBox "The document contain a TOC entry field"
End If

HTH + Cheers - Peter
 
D

DebbiePartridge

Have sorted this one thanks. Didn't want anyone to waste any time. Fo
anyone else's info, this is how I did it. There may be a sharper wa
but this does work:

Private Sub Check_Marked_Entries()
Dim curDOC As Document
Dim fldTYPE As Field
Dim intCOUNT As Integer

Set curDOC = ActiveDocument
intCOUNT = 1
booSTYLEEXISTS = False

For Each fldTYPE In curDOC.Fields
If curDOC.Fields.Item(intCOUNT).Type = wdFieldTOCEntry Then
booSTYLEEXISTS = True
Exit Sub
End If
Next

End Su
 

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