Find the word Table (or Figure) inside fields

Hi folks,

I review documents to go to FDA, and they'll typically have dozens of cross-refs to Tables and Figures. Currently I use Find and search on the word "Table" (or Figure) but it finds all of them. I'd like to only find the ones which are fields. Is there code to do this?

If you show all the field codes (Alt-F9) then you can do a search on '^d REF' to find all the cross-reference fields but that is unlikely to be what you want immediately.

I am trying to work out why you would do this search. If it is to check each result then I would try the above search and replace each of those with highlighting then toggle the fields once again and do a search for the word Table with highlighting.

I review documents to go to FDA, and they'll typically have dozens of cross-refs to Tables and Figures. Currently I use Find and search on the word "Table" (or Figure) but it finds all of them. I'd like to only find the ones which are fields. Is there code to do this?

A Find (e.g. via Ctrl-F) is incapable of differentiation what kind of container the found content resides in. If you want to find references to a particular table, include the table #, for example, as part of the Find specification.

Even a Find macro is incapable of differentiating the kind of container the found content resides in. One could use a loop that goes through all fields - and report only those that contain the relevant content - but that's the inverse of what you seem to be after. For example, the following macro will only report cross-reference fields that contain the word 'Table':

Code:

Sub Demo()
Dim Fld As Field, Rslt As Variant
For Each Fld In ActiveDocument.Fields
With Fld
If .Type = wdFieldRef Then
If InStr(.Result, "Table") Then
.Result.Select
If MsgBox("Exit Here?", vbYesNo) = vbYes Then Exit Sub
End If
End If
End With
Next
End Sub

Earlier today I was checking a document in which many of the x-refs to Tables and Figures had partially broken. Many of the captions had been numbered sequentially; some had been numbered to include chapter numbering; and some had been numbered with both, where the table numbers didn't reset with the start of a new chapter. When the author fixed them all to use the correct chapter numbering, some of the x-refs lost part of their numbering, i.e. Table 5-30 turned into Table 5-. I had to fix all of them; the first step was finding them.

More commonly I have to check documents to ensure that all the appropriate references to tables and figures are linked (these study reports have both internal tables/figures and external tables/figures; only the internal ones are linked using Word). In these cases it's useful to only find the word Table (or Figure) when it's not linked. I was hoping to adapt the answer I received here to do that search too.

In these cases it's useful to only find the word Table (or Figure) when it's not linked. I was hoping to adapt the answer I received here to do that search too.

Well, give that the code I posted only ever considers field content, as per your specs, there's not way you could adapt it to do the opposite. Quite different code would be required, such as:

Code:

Sub Demo()
Dim Fld As Field, StrRngs As String, i As Long, RsltFld As Variant, Rslt As Variant
StrRngs = "0-0,"
RsltFld = MsgBox("Find 'Table' in cross-references?", vbYesNo)
With ActiveDocument
For Each Fld In .Fields
With Fld
If .Type = wdFieldRef Then
If InStr(.Result, "Table") Then
StrRngs = StrRngs & .Result.Start & "-" & .Result.End & ","
End If
End If
End With
Next
With .Range
StrRngs = StrRngs & .End & "-" & .End
With .Find
.ClearFormatting
.Text = "Table"
.Replacement.Text = ""
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
With .Duplicate
'To test ranges within REF fields
If RsltFld = vbYes Then
For i = 1 To UBound(Split(StrRngs, ",")) - 1
If .Start >= Split(Split(StrRngs, ",")(i), "-")(0) Then
If .End <= Split(Split(StrRngs, ",")(i), "-")(1) Then
'in a REF field
.Select
If MsgBox("Exit Here?", vbYesNo) = vbYes Then Exit Sub
End If
End If
Next
End If
'To test ranges not within REF fields
If RsltFld = vbNo Then
For i = 0 To UBound(Split(StrRngs, ",")) - 1
If .Start > Split(Split(StrRngs, ",")(i), "-")(1) Then
If .End < Split(Split(StrRngs, ",")(i + 1), "-")(0) Then
'not in a REF field
.Select
If MsgBox("Exit Here?", vbYesNo) = vbYes Then Exit Sub
End If
End If
Next
End If
End With
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End With
End Sub