Consulting

Results 1 to 7 of 7

Thread: Macro to find fields with "0" result

  1. #1
    VBAX Regular YossiD's Avatar
    Joined
    Jan 2009
    Posts
    33
    Location

    Macro to find fields with "0" result

    I'm looking for a macro that will check each field in an open document to find fields that display a result of zero (0). For example, broken cross-reference fields sometimes display a result like see para 0. I'd like a quick way to find those broken cross-references so I can fix them.

    I'm sure this is a piece of cake to do, but with my extremely limited VBA knowledge, it will take me a while to figure out, and I just can't spare the time at the moment.

    TIA

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Finding them isn't the problem. What do you want the macro to do when it finds one?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular YossiD's Avatar
    Joined
    Jan 2009
    Posts
    33
    Location
    The macro can just stop when it finds the field, leaving the cursor there so I can fix the link. Then I'll run the macro again to check for more bad links until I've found them all.

    I suppose a more sophisticated macro could highlight the fields and print a message saying how many it found, but that's really not necessary as there really shouldn't be very many.

    Thanks.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You could use something like:
    Sub FindEmptyRef()
    Dim oFld As Field
    For Each oFld In ActiveDocument.Fields
      With oFld
        If (.Type = wdFieldPageRef) Or (.Type = wdFieldRef) Then
          If .Result = "0" Then .Select: Exit Sub
        End If
      End With
    Next oFld
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular YossiD's Avatar
    Joined
    Jan 2009
    Posts
    33
    Location
    I'm afraid that doesn't work.

    A quick search turned up a previous thread where I asked this question last year and you replied with a recommended solution (a bit different from this one), but that didn't work either. I must have been tied up with something else because I didn't follow up with you.

    In the meantime it seems we both forgot about it. Does that mean we're getting old or just that we're busy with too many things.

    Anyway, I'm attaching a file this time (Broken link.doc) so you can see what the bad link looks like. this is the first time attaching a file to a post so I hope it works.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    It seems the REF fields actually include a non-printing character in this case. Try:
    Sub FindEmptyRef()
        Dim oFld As Field
        For Each oFld In ActiveDocument.Fields
            With oFld
                If (.Type = wdFieldPageRef) Or (.Type = wdFieldRef) Then
                    If .Result = ChrW(8206) & "0" Then .Select: Exit Sub
                End If
            End With
        Next oFld
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Regular YossiD's Avatar
    Joined
    Jan 2009
    Posts
    33
    Location
    Works perfectly, thanks.

    How on earth did you figure out about the non-printing character?

Posting Permissions

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