View Single Post
 
Old 09-11-2014, 09:09 AM
odin odin is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Sep 2014
Posts: 11
odin is on a distinguished road
Default RichTextContentControl with text and hyperlinks via Userform possible?

I have a word form with a specific Rich Text Content Control that I'm wanting to have normal text and a hyperlink(s). I know it is possible to have both.

I created a userform with TextBox(s) that will fill some legacy formfields and a Rich Text Content Control via it's Tag. I've gotten the coding for that much to work!

I also have a userform with TextBox(s) that will create a hyperlink at the current currsor position. The hyperlink display text doesn't match the actual link which is what I'm wanting. So that code works too!

So here's my question / problem:

I'm wanting the hyperlink(s) to be in the same Rich Text Content Control along with normal text that is currently being filled with a userform. The contentcontrol isn't locked so I can currently click anywhere in the contenetcontrol right now and use my other userform to insert the hyperlink(s) inside it.

Is it possible to do all of this within just one userform?

I thought I might could combine the two userforms and have the user when typing the main contents of the contentcontrol portion, to type in the hyperlink display text where they're wanting the hyperlink(s) to be placed. Then on the hyperlink portion of the userform they'll type the hyperlink display text again in the display text box and the web address in the web address box.Then have the code when the Done button is clicked:

Place the contents in the content control as it does now. Then search the content control for the hyperlink display text entries and convert/replace them with the appropriate hyperlink(s).

I'm not sure what they best way to do this would be. Any help would be much appreciated.

Userform code that fills in the formfields and contentcontrol:

Code:
 
Private Sub cmdDone_Click()
Dim qtyBox As String
Dim markBox As String
Dim descriptionBox As String
Dim oCC As ContentControls
Set oCC = ActiveDocument.SelectContentControlsByTag("idMaterial")
'Validate key entries
  If Me.qtyBox.Value = "" Then
    MsgBox "You must enter a Quantity."
    Exit Sub
  End If
  If Me.markBox.Value = "" Then
    MsgBox "You must enter the Mark."
    Exit Sub
    End If
  If Me.descriptionBox.Value = "" Then
    MsgBox "You must enter the Description."
    Exit Sub
  End If
ActiveDocument.FormFields("qtyText").Result = Me.qtyBox.Text
ActiveDocument.FormFields("markText").Result = Me.markBox.Text
oCC(1).Range.Text = Me.descriptionBox.Text
Unload Me
lbl_Exit:
Exit Sub
End Sub
Userform code inserts hyperlink:

Code:
Private Sub cmdInsertHLink_Click()
'Declare variables
Dim dspText As String     ' Display Text for hyperlink
Dim webAddrss As String  ' Web / Desitnation Address
 
  'Validate key entries
  If Me.dspTextBox1.Value = "" Then
    MsgBox "You must enter Display text."
    Exit Sub
  End If
  If Me.webaddrssTextBox1.Value = "" Then
    MsgBox "You must enter the Web Address or Link."
    Exit Sub
  End If
 
  'Write the hyperlink
  ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        Me.webaddrssTextBox1.Text, SubAddress:="", ScreenTip:="", TextToDisplay:= _
        Me.dspTextBox1.Text
  'Update fields and toggle codes.
  With ActiveDocument.Fields
    .ToggleShowCodes
    .Update
  End With
Unload Me
lbl_Exit:
Exit Sub
End Sub
I'm open to other ideas and/or suggestions on the best way to accomplish the end result I'm looking for.

Last edited by odin; 09-11-2014 at 10:53 AM.
Reply With Quote