PDA

View Full Version : [SOLVED:] Macro to find fields with "0" result



YossiD
07-08-2014, 02:23 AM
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

macropod
07-08-2014, 03:52 AM
Finding them isn't the problem. What do you want the macro to do when it finds one?

YossiD
07-08-2014, 04:21 AM
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.

macropod
07-08-2014, 05:09 AM
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

YossiD
07-08-2014, 06:24 AM
I'm afraid that doesn't work.

A quick search turned up a previous thread (http://www.vbaexpress.com/forum/archive/index.php/t-45228.html) 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 (11910) 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.

macropod
07-08-2014, 06:53 AM
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

YossiD
07-08-2014, 07:46 AM
Works perfectly, thanks.

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