Andreas Hermle
asked on
Detect invalid cross-references using VBA
Dear Experts:
I wonder whether it is possible by means of VBA ...
... to validate a REF-Field, ie. whether the Reference Source exists or not (can be found or not).
In the current document ...
... I created a lot of cross-references that all point to bookmarked text.
... e.g. {REF bookmark_90 \h} or {REF bookmark_91 \h}
... These cross-references are all listed in a (1) table
There are lots of bookmarks where the reference source could not be found, ie. cross-references that point to non-existing bookmarks.
Is it possible to run a macro that ...
... detects the row of the current table where the first error occurs and then
... sets a range till the end of the current table and ...
... deletes these rows (this range)
There are never any valid references in between, ie. if the first error ( Error: Reference Source could not be found!) occurs, it continues down to the last row.
This screenshot concisely explains my problem
Reference-Source-not-found.tif
Help is much appreciated. Thank you very much in advance.
Regards, Andreas
I wonder whether it is possible by means of VBA ...
... to validate a REF-Field, ie. whether the Reference Source exists or not (can be found or not).
In the current document ...
... I created a lot of cross-references that all point to bookmarked text.
... e.g. {REF bookmark_90 \h} or {REF bookmark_91 \h}
... These cross-references are all listed in a (1) table
There are lots of bookmarks where the reference source could not be found, ie. cross-references that point to non-existing bookmarks.
Is it possible to run a macro that ...
... detects the row of the current table where the first error occurs and then
... sets a range till the end of the current table and ...
... deletes these rows (this range)
There are never any valid references in between, ie. if the first error ( Error: Reference Source could not be found!) occurs, it continues down to the last row.
This screenshot concisely explains my problem
Reference-Source-not-found.tif
Help is much appreciated. Thank you very much in advance.
Regards, Andreas
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Rgonzo,
I came up with my own code, but as a matter of act I can detect no increase in speed.
I have many more fields in this document and below macro still seems to iterate thru all the fields in the whole document.
I came up with my own code, but as a matter of act I can detect no increase in speed.
I have many more fields in this document and below macro still seems to iterate thru all the fields in the whole document.
Sub Macro_2()
Dim fld As Field
Dim tbl As Table
For Each fld In ActiveDocument.Tables(1).Range.Fields
If Split(fld.Result.Text, " ")(0) = "Fehler!" Then
fld.Result.rows.Delete
End If
Next
End Sub
Hi,
Your code could be quicker by inhibiting the screenupdating
Your code could be quicker by inhibiting the screenupdating
Sub Macro_3()
Dim fld As Field
Dim tbl As Table
Application.ScreenUpdating = False
For Each fld In ActiveDocument.Tables(1).Range.Fields
If Split(fld.Result.Text, " ")(0) = "Fehler!" Then
fld.Result.Rows.Delete
End If
Next
Application.ScreenUpdating = True
End Sub
ASKER
Ok, I'll give it a try and let you know then.
Regards, Andreas
Regards, Andreas
ASKER
Ok, this sped it up considerably. Thank you very much. Now comes the points awarding.
Regards, Andreas
Regards, Andreas
ASKER
Thank you very much for your professional support. Regards, Andreas
Hi,
As bonus here the code where it deletes from the first where it finds an error until the end of the table in one go
As bonus here the code where it deletes from the first where it finds an error until the end of the table in one go
Sub Macro_4()
Dim fld As Field
Dim tbl As Table
For Each fld In ActiveDocument.Tables(1).Range.Fields
If Split(fld.Result.Text, " ")(0) = "Fehler!" Then
Idx = fld.Code.Cells(1).Row.Index
EndOfTable = ActiveDocument.Tables(1).Rows.Count
With ActiveDocument.Tables(1)
Set rngRowsToBeDeleted = .Rows(Idx).Range
rngRowsToBeDeleted.End = .Rows(EndOfTable).Range.End
End With
rngRowsToBeDeleted.Rows.Delete
Exit For
End If
Next
End Sub
Regards
ASKER
Hi Rgonzo,
that's very nice of you. I will give it a try and let you know.
Thank you very much for your great, professional and swift help.
Regards, Andreas
that's very nice of you. I will give it a try and let you know.
Thank you very much for your great, professional and swift help.
Regards, Andreas
ASKER
Hi Rgonzo,
great, this works just fine and it is quicker to perform.
Thank you very much for your great and professional support.
Andreas
great, this works just fine and it is quicker to perform.
Thank you very much for your great and professional support.
Andreas
ASKER
great this did the trick! Thank you very much for your swift and professional support.
There is one thing I still would like to address:
Is it possible to restrict this row deleting to ....
... just the currently selected table, which happens to be the first table in the document.
Regards, Andreas