Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Detect invalid cross-references using VBA

Posted on 2013-11-06
10
Medium Priority
?
995 Views
Last Modified: 2013-11-11
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
0
Comment
Question by:AndreasHermle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 39626733
Hi,

pls try
Sub Macro()
'
For Each fld In ActiveDocument.Fields
    If Split(fld.Result.Text, " ")(0) = "Fehler!" Then
        fld.Result.Rows.Delete
    End If
Next
End Sub

Open in new window

Regards
0
 

Author Comment

by:AndreasHermle
ID: 39626974
Hi Rgonzo,

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
0
 

Author Comment

by:AndreasHermle
ID: 39627007
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.


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

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 39627069
Hi,

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

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 39627325
Ok, I'll give it a try and let you know then.

Regards, Andreas
0
 

Author Comment

by:AndreasHermle
ID: 39629682
Ok, this sped it up considerably. Thank you very much. Now comes the points awarding.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 39629685
Thank you very much for your professional support. Regards, Andreas
0
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 39630000
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

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

Open in new window

Regards
0
 

Author Comment

by:AndreasHermle
ID: 39634852
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
0
 

Author Comment

by:AndreasHermle
ID: 39638251
Hi Rgonzo,

great, this works just fine and it is quicker to perform.

Thank you very much for your great and professional support.

Andreas
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question