[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1267
  • Last Modified:

Add a hyperlink to a content control

Hi experts

I have a document which is populated from a User Form. Data entered in the user form is placed into locked content controls. I do this via a function to unlock the content control, add the data in, then re-lock the content control. This forces users to enter their data via the User Form.

Now I have a situation where my content control needs to be populated with a hyperlink, but I keep getting an out-of-memory error.

How can I get this to work???

I'm calling the function from the following line of code in my UserForm. "Permalink" is the title I've given my content control.

AddCCHyperlink "Permalink", Me.txtPermalink


Function AddCCHyperlink(sCCTitle As String, sLink As String)

    Dim CC As ContentControl
    Dim rng As Range
    
    For Each CC In ActiveDocument.ContentControls
        If CC.Title = sCCTitle Then
            CC.LockContentControl = False
            CC.LockContents = False
            Set rng = CC.Range
            'ActiveDocument.Hyperlinks.Add Anchor:=rng, Address:=sLink 'doesn't work
             CC.Range.Hyperlinks.Add Anchor:=CC, Address:=sLink 'doesn't work
            'rng.Select
            'Selection.Hyperlinks.Add Anchor:=Selection.Range, Address:=sLink 'doesn't work
            CC.LockContentControl = True
            CC.LockContents = True
        End If
    Next
End Function

Open in new window

0
Fi69
Asked:
Fi69
1 Solution
 
GrahamSkanCommented:
I can't reproduce the loop, but I do get  Command Failed error, but not if we use the Range instead of the control itself as the Anchor.

Incidentally the document object has a SelectContentControlsByTitle method which works as shown

Function AddCCHyperlink(sCCTitle As String, sLink As String)
    Dim CCS As ContentControls
    Dim CC As ContentControl
    Dim rng As Range
    
    Set CCS = ActiveDocument.SelectContentControlsByTitle(sCCTitle)
    Set CC = CCS(1)
    
    CC.LockContentControl = False
    CC.LockContents = False
    
    Set rng = CC.Range
    CC.Range.Hyperlinks.Add Anchor:=rng, Address:=sLink
    
    CC.LockContentControl = True
    CC.LockContents = True
End Function

Open in new window

0
 
Fi69Author Commented:
Hi Graham

Thanks for the response.

Your code above sort-of works. If I use it as is then it doesn't produce an error, however, it doesn't update the text displayed, ie if there is the hyperlink display text already in the content control, it doesn't update the display text. So I amended your line 13 to:

cc.range.hyperlinks.add Anchor:=rng, Address:=sLink, TextToDisplay:=slink

And I get the out of memory error again.

I tried deleting the existing text in the range, but error remains.

Any ideas what's going on?
0
 
GrahamSkanCommented:
I get the same symptom (Word 2007) if there has already been a hyperlink there. It looks like a bug. So far I haven't found a way around it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
irudykCommented:
Does deleting the existing hyperlink first, before you add the new hyperlink get around the issue?
Function AddCCHyperlink(sCCTitle As String, sLink As String)
    Dim CCS As ContentControls
    Dim CC As ContentControl
    Dim rng As Range
    
    Set CCS = ActiveDocument.SelectContentControlsByTitle(sCCTitle)
    Set CC = CCS(1)
    
    CC.LockContentControl = False
    CC.LockContents = False
    
    Set rng = CC.Range

    If rng.Hyperlinks.Count > 0 Then rng.Hyperlinks(1).Delete

    CC.Range.Hyperlinks.Add Anchor:=rng, Address:=sLink
    
    CC.LockContentControl = True
    CC.LockContents = True
End Function

Open in new window

0
 
Fi69Author Commented:
Thanks Graham and irudyk

Unfortunately irudyk's code doesn't work either. I've ended up working around the situation by deleting the content control and then adding it back in.

Function AddCCHyperlink(sCCTitle As String, sLink As String)
    Dim CCS As ContentControls
    Dim CC As ContentControl
    Dim rng As Range
    
    Set CCS = ActiveDocument.SelectContentControlsByTitle(sCCTitle)
    Set CC = CCS(1)
    
    CC.LockContentControl = False
    CC.LockContents = False
    
    Set rng = CC.Range
    rng.Delete
    
    If sLink <> "" Then
        CC.Delete 'delete the content control
        rng.Hyperlinks.Add rng, sLink
        rng.Select
        Do Until rng.Hyperlinks.Count = 1
            rng.MoveStart wdCharacter, 1
        Loop
        Set rng = rng.Hyperlinks(1).Range
        Set CC = rng.ContentControls.Add(wdContentControlRichText) 'add back in content control
        CC.Title = sCCTitle
    End If
    
    CC.LockContentControl = True
    CC.LockContents = True

End Function

Open in new window

0
 
Martin LissRetired ProgrammerCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now