Microsoft Word Add-in Hyperlink Object with VBA code example

Microsoft Word Add-in Hyperlink Object with VBA code example

Hyperlink

Is a object refers to create link to navigate target location. Hyperlink is very useful object, allows to attach link which can be pointed to other location and upon follow the location can be located. Microsoft Word offers Hyperlinks collection exposed by document object which can be iterated using index (a positive integer). Hyperlink offers various methods and properties as listed below:

ADD

Method can be used to insert or attach a new hyperlink in the document. Below code will select a shape and add hyperlink to it.

Syntax

expression.Add(Ancher, [Address], [SubAddress], [ScreenTip], [TextToDisplay], [Target])

Code example

Public Sub AddHyperLinkIn()
    'Document object
    Dim oDocument As Document
    'bind active document
    Set oDocument = ActiveDocument
    
    'Shape object
    Dim oShape As Shape
    'Bind first shape in the document
    Set oShape = oDocument.Shapes(1)
    
    'Hyperlink object
    Dim hyperLink As hyperLink
    
    'Create new hyperlink
    Set hyperLink = Selection.Hyperlinks.Add(oShape, "https:\\www.vbaoverall.com")
    
    'CleanUp
    If Not oShape Is Nothing Then
        Set oShape = Nothing
    End If
    If Not hyperLink Is Nothing Then
        Set hyperLink = Nothing
    End If
    If Not oDocument Is Nothing Then
        Set oDocument = Nothing
    End If
End Sub

Output

Follow

Method allows to auto navigate target location attached to given object. In this example we will trigger follow method to navigate https://www.vbaoverall.com as shown in above figure using code.

Code example

Public Sub FollowHyperLink()
    'Document object
    Dim oDocument As Document
    'bind active document
    Set oDocument = ActiveDocument
    
    'Shape object
    Dim oShape As Shape
    'Bind first shape in the document
    Set oShape = oDocument.Shapes(1)
    
    'Click on Hyperlink
    oShape.hyperLink.Follow
    
    'CleanUp
    If Not oShape Is Nothing Then
        Set oShape = Nothing
    End If
    If Not oDocument Is Nothing Then
        Set oDocument = Nothing
    End If
End Sub

Output

Count

Is a property, returns available number of hyperlinks in a document. Below code print total number of hyperlinks.

Code example

Public Sub CountHyperlinks()
    'Document object
    Dim oDocument As Document
    'bind active document
    Set oDocument = ActiveDocument
    
    'Print count
    Debug.Print "There are total " & oDocument.Hyperlinks.Count & " hyperlink(s) in your document"
    
     'CleanUp
    If Not oDocument Is Nothing Then
        Set oDocument = Nothing
    End If
End Sub

Output

There are total 1 hyperlink(s) in your document

Hyperlinks

Is a collection contains all available hyperlinks in a document. Below code prints address of all available hyperlinks using for each loop.

Code example

Public Sub ListAllHyperLinks()
    'Document object
    Dim oDocument As Document
    'bind active document
    Set oDocument = ActiveDocument
    
    'Object for hyperlink
    Dim oHyperLink As hyperLink
    
    'Iterate each hyperlink
    For Each oHyperLink In oDocument.Hyperlinks
        Debug.Print oHyperLink.Address
    Next oHyperLink
    
    'CleanUp
    If Not oDocument Is Nothing Then
        Set oDocument = Nothing
    End If
    If Not oHyperLink Is Nothing Then
        Set oHyperLink = Nothing
    End If
End Sub

Output

https://www.vbaoverall.com

Next >> Microsoft Word Frame Object with VBA code example

Leave a Reply

Your email address will not be published. Required fields are marked *