Word 2013: detect and delete cross reference codes that are not valid. In other words (Error! Reference Source not Found.)

We have a Trust Document that has multiple combinations of Articles. For example:

Article 4, 5 & 6 have four different types of Article 4, 5 & 6. So, I cross reference each type to accommodate any choice that is made by the user.  The resulting document contains many of the following example:
Error! Reference source not found.Error! Reference source not found.ARTICLE 5

I have created a simple macro that finds Error! Reference source not found. and replaces it with nothing. Although in the document the cross reference codes appear as if they are gone (no spaces left - nothing) they really are still there. I know this because when I create a table of contents in the finished document, the "Error! Reference source not found." reappears in the document. Also, you can press Alt+F9 and see the codes that should have been gone.

Question:
How do I remove only those cross reference codes that are invalid?

Thanks so much to this brain trust.
LegalLearningCenterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DrTribosCommented:
Hi there...

So - the cross ref is a field code that displays information about a bookmark.  for example it could display the paragraph number, the text, or what page it is on.

The error you see happens when a bookmark is deleted.  The field has nothing to display so it displays an error.  Some fields contain sub-fields so a ref that should be "Ch1-Article1" might show as "Ch1-Error...."

You can fix with VBA... you need to delete the entire field (or find a way to regenerate it).

Can you share the code you have so far...?
GrahamSkanRetiredCommented:
As Steve indicates, this can happen if the referenced object has been deleted. This macro has been tested against missing bookmarks. If it doesn't work in every case, we might need a to see a sample document.
Sub DeleteBlindRef()
    Dim i As Integer
    Dim fld As Field
    
    With ActiveDocument
        .Fields.Update 'ensure that fields display is current
        'For Each fld In .Fields 'this construct is unreliable if items are to be deleted from the collection
        For i = .Fields.Count To 1 Step -1 'can be slower than For Each
            Set fld = .Fields(i)
            If fld.Type = wdFieldRef Then
                If InStr(fld.Result, "Error! Reference source not found.") > 0 Then
                    fld.Delete
                End If
            End If
        Next i
    End With
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LegalLearningCenterAuthor Commented:
Graham, This worked perfectly!  Thank you so much. This document is complex in that there are so many combinations of paragraphs that the user might choose.  In some paragraphs I have up to 4 different cross references and of course only one would be valid.  I work in law firms exclusively and they don't use Word like a regular person would!  Thanks again!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.