• Word 2016 VBA to remove cross references and end the “Error! Reference source not found”

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Word 2016 VBA to remove cross references and end the “Error! Reference source not found”

    Author
    Topic
    #507708

    I’m a total newbie here. I have Word Document that has 15-20 pages with most information repeating. Over 20 fields are repetitive. I used bookmarks and cross-references to generate all the subsequent pages following the first two pages of just a table and bookmarks. My problem is that I cannot save the final version with the table of bookmarks in it. I have to delete those two pages. Deleting those pages means all the content becomes “Error! Reference source not found” when someone prints or saves.

    First, I tried this script that I found on a website. The code actually meant I could SAVE the document and the data stayed intact, but would go to the Error! when printing:

    Code:
    Sub removebookmarks()
    Dim bkm As Bookmark
    For Each bkm In ActiveDocument.Bookmarks
    bkm.Delete
    Next bkm
    End Sub
    

    Then, I tried these two scripts I found here on the forum hoping this would just drop the cross-references and keep the original data intact, but the data was still lost when printing and Error! on any PC that tried printing the document:

    Code:
    Sub UnlinkBookmarks()
        Dim aFld As Field
            For Each aFld In ActiveDocument.Fields
                If InStr(1, aFld.Code.Text, “LSU_”, 1) Then
                    ‘Debug.Print aFld.Code.Text
                    aFld.Unlink
                End If
        Next aFld
    End Sub
    
    Code:
    Sub UnlinkBookmarks()
    Dim i as integer
    For i = ActiveDocument.Fields.count to 1 step -1
      If InStr(1, aFld.Code.Text, “LSU_”, 1) > 0 then
        ActiveDocument.Fields(i).unlink
      end if
    Next i
    End Sub
    

    Unfortunately, I have the same “Error!…” coming up.

    I’m totally clueless with VBA or any of this scripting. I’ve been reading and reading to no avail.

    It’s entirely fine if all the hyperlinks and what not are removed. The only “links” are between bookmarks and cross-references. I just need the data to stay intact through all of the sections of this document and to remove those links so I can delete the bookmark “document generator” table as I like to call it.

    I appreciate any help out there.

    Viewing 1 reply thread
    Author
    Replies
    • #1586221

      I just realized in my ignorance and haste that the code I used references LSU_. So that would be a negative for removing my cross references. Ugh! Glad I’m drinking coffee now.

      Anyway, I just stripped down the code to remove the field LSU_ stuff to something like this below to remove any and all cross-references. But, it didn’t work. What am I missing to do the removal of the cross-reference while keeping the original data from the bookmark intact at that spot?

      Code:
      Sub UnlinkBookmarks()
          Dim aFld As Field
              For Each aFld In ActiveDocument.Fields
                      aFld.Unlink
                  End
          Next aFld
      End Sub
      
    • #1586693

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    Viewing 1 reply thread
    Reply To: Word 2016 VBA to remove cross references and end the “Error! Reference source not found”

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: