Fi69
asked on
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
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
ASKER
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?
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?
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.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Incidentally the document object has a SelectContentControlsByTit
Open in new window