MS Word Hyperlink tag removed after field unlink

Zhu, Ligong 260 Reputation points
2025-06-06T18:26:30.0566667+00:00

Hi, We use VBA code to populate many docvariables at runtime. Recently we added a "Hyperlink" tag to a static url link in our document for 508 compliance purpose. During VBA field update, we realize that this VBA line ("Unlink") actually would delete this hyperlink tag from generated Word document:

ActiveDocument.Fields.Update

ActiveDocument.Fields.Unlink

All other regular docvariable fields are populated correctly.

Without this Unlink line, the generated document will contain hidden docvariables, which we don't want to have.

You can see a sample document image containing a hyperlink tag, which will be deleted after Unlink code.

Is there anything we can do to keep Hyperlink tag while still populate docvariable correctly?

Thanks.

Screenshot 2025-06-06 142201

Microsoft 365 and Office | Word | For business | Windows
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jay Freedman 201 Reputation points Volunteer Moderator
    2025-06-08T01:25:15.2033333+00:00

    To avoid removing the HYPERLINK keyword, you'll have to loop through the ActiveDocument.Fields collection and test each field's Type property. If it's wdFieldHyperlink, then don't execute the Unlink for that field:

    Dim fld As Field
    For Each fld In ActiveDocument.Fields
    If Not fld.Type = wdFieldHyperlink Then
    fld.Unlink
    End If
    Next fld

    If your documents could contain hyperlinks in storyranges other than the main one, you'll have to code for those ranges (headers, footers, footnotes, endnotes, textboxes/shapes) separately.

    1 person found this answer helpful.

  2. Jay Freedman 201 Reputation points Volunteer Moderator
    2025-06-12T19:16:29.8133333+00:00
    Sub UnlinkFieldsExceptHyperlinks()
        Dim fld As Field
        Dim rgInner As Range
        Dim fldInner As Field
        
        For Each fld In ActiveDocument.Fields
            Select Case fld.Type
                Case wdFieldHyperlink
                    'skip it
                    GoTo SkipHyper
                
                ' in the next Case statement you can add other
                ' field types that could hide a hyperlink,
                ' such as wdFieldQuote, separated by commas
                Case wdFieldAutoText, wdFieldIf
                    Set rgInner = fld.Code
                    For Each fldInner In rgInner.Fields
                        If fldInner.Result.Hyperlinks.Count = 0 Then fldInner.Unlink
                    Next fldInner
                
                ' unlink any other kind of field
                Case Else
                    fld.Unlink
    SkipHyper:
                ' give Windows control to avoid "not responding" state
                DoEvents
            End Select
        Next fld
        
        ' release the local object
        Set rgInner = Nothing
    End Sub
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.