Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Aug 2015
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA Unlink fields which only relate to bookmarks

    Please could somebody help me out? I have been browsing the forum for what seems like an eternity but I can't seem to find an answer to my question.

    Is there a way I could use VBA to unlink only those fields which refer to a bookmark? In doing so, I am hoping my cross references to numbered items would remain intact..

    I was hoping I could add wording to the code previously provided by Andrew Lockton:
    Code:
    Dim aFld As Field
    For Each aFld In ActiveDocument.Fields
      If aFld.Type = wdFieldDocVariable Then aFld.Unlink
    Next aFld
    Thank you very much in advance!

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Word cross-references to numbered items are ACTUALLY created by using the same 'add a bookmark then insert a field that links to that bookmark' that you are trying to identify. So you need to be very sure about what exactly you are trying to unlink. The following is an edu-ma-cated guess at identifying exactly what you wanted. We would need to examine all your possible field codes to identify a better pattern to use but the general principle would remain the same.

    The biggest difference between links to manually inserted bookmarks and links to automatically inserted bookmarks is the presence of an underscore at the start of the bookmark name so we could code a solution that looks at the field codes to avoid unlinking Ref fields to automatically inserted bookmarks.
    Code:
    Sub Macro1()
      Dim aFld As Field, sCode As String
      For Each aFld In ActiveDocument.Fields
        sCode = LCase(aFld.Code)
        Debug.Print sCode
        If InStr(sCode, "ref") > 0 And Not InStr(sCode, "ref _") > 0 Then
          aFld.Unlink
        End If
      Next aFld
    End Sub
    Note that field codes don't care about case (ie REF = ref = Ref) but the InStr function certainly does. That is why I converted the field code to lowercase.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    New Lounger
    Join Date
    Aug 2015
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Andrew - thank you very much for your reply. Wouldn't it make sense then to start each bookmark which I use for these purposes to start with a unique string, e.g. "LSU_[Bookmark name]" and look for bookmarks that start with that string?

    This is probably very easy for experienced VBA users, but it still helps me loads!

    Seems to work:
    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
    Many thanks!
    Last edited by LSU; 2015-08-20 at 03:21. Reason: Added example code

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Yes, it would make a lot of sense to do that. I was assuming that the bookmarks and cross-refs already existed in the document but if you are yet to add them then your suggestion is easily the best way to do it. It appears from your amendments that you already understand enough about this to get a good working solution. InStr returns an integer not a boolean but anything other than zero is treated as true so you can get away with coding the if statement that way.

    I might suggest you do a bit of testing though because VBA loops have a bad habit of handling collections poorly if you remove items while you are looping them. If we unlink a field, we now have one less field in the document so the collection has shrunk by one. The 'next' field might get skipped since the stack is now one smaller. This doesn't appear to be a problem with this code but it is certainly something to be aware. The usual workaround for that problem is to count backwards from the last item so that removing one doesn't affect the order of the remaining items. eg
    Code:
    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
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •