I am trying to create a macro 3 different choices for updating fields within a word doc. I am able to update all of the fields including the ToC and links throughout or the ToC only, but would also like to be able to only update fields that are not the ToC. Is there a way to select everything but the ToC and then update? Here is the current code I'm running:
Sub RefreshFields()
'
' RefreshFields Macro
'
'
Dim ToC As TableOfContents
Select Case InputBox("Update which fields?" & vbCrLf & "Type '1' for all fields" & vbCrLf & "Type '2' for Table of Contents only" & vbCrLf & "Type '3' for all fields except Table of Contents")
Case 1
Selection.WholeStory
Selection.Range.HighlightColorIndex = wdYellow
Selection.Fields.Update
MsgBox ("All fields updated")
Case 2
For Each ToC In ActiveDocument.TablesOfContents
ToC.Update
Next
MsgBox ("Only ToC updated")
Case 3
'
'
' MsgBox ("All fields except ToC updated")
End Select
End Sub
I tried different function to select the table of contents and pair that with some kind of "except" function but was unable to.
Option Explicit
Sub RefreshFields()
Dim fld As Field
Select Case InputBox("Update which fields?" & vbCrLf & "Type '1' for all fields" & vbCrLf & "Type '2' for Table of Contents only" & vbCrLf & "Type '3' for all fields except Table of Contents")
Case 1
Selection.WholeStory
Selection.Range.HighlightColorIndex = wdYellow
Selection.Fields.Update
MsgBox ("All fields updated")
Case 2
Dim ToC As TableOfContents
For Each ToC In ActiveDocument.TablesOfContents
ToC.Update
Next
MsgBox ("Only ToC updated")
Case 3
For Each fld In ActiveDocument.Fields
If Not IsFieldInTOC(fld) Then
fld.Update
End If
Next
MsgBox ("All fields except ToC updated")
End Select
End Sub
Function IsFieldInTOC(fld As Field) As Boolean
Dim ToC As TableOfContents
Dim fldStart As Long
Dim fldEnd As Long
fldStart = fld.Result.Start
fldEnd = fld.Result.End
For Each ToC In ActiveDocument.TablesOfContents
If (fldStart >= ToC.Range.Start And fldStart <= ToC.Range.End) _
Or (fldEnd >= ToC.Range.Start And fldEnd <= ToC.Range.End) Then
IsFieldInTOC = True
Exit Function
End If
Next ToC
IsFieldInTOC = False
End Function