Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    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?

    A Google search failed me.

    Thanks in advance,

    Beej

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,507
    Thanks
    3
    Thanked 142 Times in 135 Posts
    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.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,855
    Thanks
    0
    Thanked 177 Times in 163 Posts
    Quote Originally Posted by ItsjustBeej View Post
    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
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. The Following User Says Thank You to macropod For This Useful Post:

    ItsjustBeej (2014-08-30)

  6. #4
    New Lounger
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks Paul. I think that will work--I'll try it out next week.

    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.

    Thanks!

  7. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,855
    Thanks
    0
    Thanked 177 Times in 163 Posts
    Quote Originally Posted by ItsjustBeej View Post
    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
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •