Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 12-02-2020, 08:02 AM
Charles Kenyon Charles Kenyon is offline Macro to delete cross-reference error message when bookmark deleted Windows 10 Macro to delete cross-reference error message when bookmark deleted Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,533
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

I have the habit, when I see excellent code, to save it for future use and perhaps tweak it. In this case, I realize that this works when the fields have been updated. This requires an extra step, which may be desirable to actually see the broken Ref fields before deleting them. Here is code that should delete any broken Ref fields even if you do not see the error message because you have not updated the fields.

I added a step to update Ref fields when located.


See following post for corrected code.


Code:
Sub RefBrokenRemove2()
'Graham Mayor - https://www.gmayor.com - Last updated - 02 Dec 2020
'https://www.msofficeforums.com/word/46089-macro-delete-cross-reference-error-message-when.html
' Remove broken Cross-Reference Fields
' Modified by Charles Kenyon to first update ref fields
'
    Dim i As Integer
    Dim oStory As range
    For Each oStory In ActiveDocument.StoryRanges
        For i = oStory.Fields.Count To 1 Step -1
            If oStory.Fields(i).Type = wdFieldRef Then
                oStory.Fields(i).Update
                If oStory.Fields(i).Result = "Error! Reference source not found." Then oStory.Fields(i).Delete
            End If
        Next i
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                For i = oStory.Fields.Count To 1 Step -1
            If oStory.Fields(i).Type = wdFieldRef Then
                oStory.Fields(i).Update
                If oStory.Fields(i).Result = "Error! Reference source not found." Then oStory.Fields(i).Delete
            End If
            '        End If   ' This line commented out to correct code
                Next i
            Wend
        End If
    Next oStory
lbl_Exit:
    Set oStory = Nothing
    Exit Sub
End Sub

Last edited by Charles Kenyon; 12-03-2020 at 10:52 AM.
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to delete cross-reference error message when bookmark deleted Find {text} and insert cross reference from bookmark Slamzor Word VBA 1 12-01-2017 05:12 PM
Macro to delete cross-reference error message when bookmark deleted Hide error message from missing bookmark lodi123 Word 2 03-28-2017 11:24 PM
Macro to delete cross-reference error message when bookmark deleted Cross-reference does not maintain formatting from bookmark frannie Word 2 03-23-2017 04:33 PM
Macro to delete cross-reference error message when bookmark deleted Weird issue with bookmark and cross-reference Maddog32 Word 2 06-10-2016 02:39 PM
Macro to delete cross-reference error message when bookmark deleted Need help with using bookmark and cross-reference mpdsal Word 1 07-26-2012 01:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:07 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft