How can I find all "reference source not found" error fields in the doc?

Great to create cross references. Common to edit document. Disasterous to remove a reference source. Result is “Error: Reference source not found.” I would like to sweep a document on the final edit pass to discover all cases where the cross reference source has been moved or removed, leaving the cross-reference field in an error state. How can all of these be found? Or in the alternative, how can I confirm that the doc is clean?

There is no convenient way to find all locations where a missing “source” is referenced.

My suggestion is to enable View>Field Shadings so that all “technical” locations in your document are highlighted (light gray). Then examine visually all gray areas, pricipally those containing “Error: Reference source not found”.

When you hover the mouse over the error, a tooltip displays the name of the missing cross-reference.

To show the community your question has been answered, click the ✓ next to the correct answer, and “upvote” by clicking on the ^ arrow of any helpful answers. These are the mechanisms for communicating the quality of the Q&A on this site. Thanks!

Hm. That might be a valuable future feature. It certainly would be valuable to me. This document is more than 300 pages.

This code may help:

Sub findDeleteBrokenCrossReferenceLinks()
doc = ThisComponent
tfs = doc.TextFields
refMs = doc.ReferenceMarks
For Each tf in tfs
  If tf.supportsService("com.sun.star.text.textfield.GetReference") OR _
     tf.supportsService("com.sun.star.text.TextField.GetReference") Then
     REM Outdated service name (case)
    If NOT refMs.hasByName(tf.SourceName) Then tf.dispose()
  End If
Next tf
End Sub

Edited (simplified): The special user function wasn’t needed.
Edited (for backwards compatibility): There was an outdated case-version of the service.

If you try it, please do so cautiously using a copy of your document, and verify the results for some cases interactively. The responsibility will completely be yours. In specific I don’t claim there can’t be textfields of a type also supporting the tested service .GetReference , but not actually being cross-reference fields of the kind you want to remove if the referenece is broken.

[Edit 2021-07-13 about 19:40 GMT]
For a demo containing an enhanced (next to final) version of the macro see attachment.
crossReferenceExample.odt

thanks so much. I will give that a try.

Hi,

It’s better to find broken references rather to delete automatically:


Sub findBrokenCrossReferenceLinks()
 doc = ThisComponent
 tfs = doc.TextFields
 oView = doc.CurrentController
 For Each tf in tfs
 	If tf.supportsService("com.sun.star.text.textfield.GetReference") OR _
 	   tf.supportsService("com.sun.star.text.TextField.GetReference") Then
 	    if tf.SourceName = "" Then
 	       oView.select(tf.Anchor)
 	       If Msgbox("Reference is broken. Do You want to edit it?", 4, "Error")  = 6 Then Exit For
 	   End If
 	End If
 Next tf
 Msgbox("No more broken references",,"Work is done")
 End Sub
1 Like