#1
|
|||
|
|||
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 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 Last edited by odin; 09-11-2014 at 10:53 AM. |
#2
|
||||
|
||||
Quote:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Quote:
|
#4
|
||||
|
||||
To do it all via the userform would entail:
1. replicating the functionality of the Insert Hyperlink dialog in the userform; 2. using either: (a) separate textboxes for the text input before & after the hyperlink; or (b) a character sequence in the input text to indicate where the hyperlink should go, as well as the one for the hyperlink itself If you make the userform modeless (by setting the form's 'ShowModal' property to false), however, the user can click in the document and add the hyperlink (using Word's own tools) to whatever gets added via the userform. However, that also means they can make other edits too, unless you protect everything else.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Ok...I've got pretty much everythings setup but I can't seem to get the hyperlink portion of the code to work. I've tried it a few ways but I keep getting an error.
Compil error: Expected: end of statement Tried this: Code:
.Cell(webIndex + 1, 3).Range.End -1 = ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=oObjForm.webaddTextBox.Text, _ SubAddress:="", ScreenTip:="", TextToDisplay:=oObjForm.dspTextBox.Text Code:
webText = ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=oObjForm.webaddTextBox.Text, _ SubAddress:="", ScreenTip:="", TextToDisplay:=oObjForm.dspTextBox.Text .Cell(webIndex + 1, 3).Range.End -1 = webText Code:
Sub insertLink() Dim dspTxt As String Dim webTxt As String ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _ "http://www.microsoft.com", SubAddress:="", ScreenTip:="", TextToDisplay:="Testing Link" |
#6
|
|||
|
|||
It is difficult to help you without seeing the full code. For example, you posted:
Code:
Sub insertLink() Dim dspTxt As String Dim webTxt As String ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _ "http://www.microsoft.com", SubAddress:="", ScreenTip:="", TextToDisplay:="Testing Link" Code:
.Cell(webIndex + 1, 3).Range.End -1 = ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=oObjForm.webaddTextBox.Text, _ SubAddress:="", ScreenTip:="", TextToDisplay:=oObjForm.dspTextBox.Text What is your full code? |
#7
|
|||
|
|||
Greg,
I'm sorry the code was incomplete and cryptic. I want to thank you for your website, it's been a lot of help and a LOT of information. The main code I'm trying to get to work is 95% from your site. I'm still testing the whole code, so there may be other errors. The code that works without any issue for inserting a hyperlink is: Code:
Sub insertLink() Dim dspTxt As String Dim webTxt As String dspTxt = "testing Link" webTxt = "http://www.microsoft.com" 'Write the hyperlink ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _ webTxt, SubAddress:="", ScreenTip:="", TextToDisplay:= _ dspTxt End Sub Here is the whole code below that I'm trying to work out: Code:
Option Explicit Public oCol As Collection Sub AddMaterialCollection() Dim oFrmInput As fmAddMaterial, oObjForm As Object Dim bRepeat As Boolean Dim oCol As Collection Dim oTbl As Table Dim lngIndex As Long Dim webIndex As Long 'Initialize the collection. Set oCol = New Collection 'Set up loop to collect repeating section information from document user. bRepeat = True Do While bRepeat Set oFrmInput = New fmAddMaterial With oFrmInput .StartUpPosition = 0 .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width) .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height) .Show End With If oFrmInput.Tag <> "USER CANCEL" Then 'Add the userform objects to the collection. oCol.Add oFrmInput Else Exit Do End If bRepeat = oFrmInput.Tag Loop 'Targets document table and output information. Set oTbl = ActiveDocument.Tables(1) With oTbl 'Add data to table. 'On Error GoTo Err_NoRecords For lngIndex = 1 To oCol.Count Set oObjForm = oCol.Item(lngIndex) .Cell(lngIndex + 1, 1).Range.Text = oObjForm.qtyBox.Text .Cell(lngIndex + 1, 2).Range.Text = oObjForm.markBox.Text .Cell(lngIndex + 1, 3).Range.Text = oObjForm.descBox.Text & " " & "FABRICATE AS SHOWN: " Set oObjForm = Nothing 'Next lngIndex 'End With With oTbl 'Add Links to end of table. On Error GoTo Err_NoRecords For webIndex = 0 To oCol.Count Set oObjForm = oCol.Item(webIndex) .Cell(webIndex + 1, 3).Range.End -1 = ActiveDocument.Hyperlinks.Add(oObjForm.webaddTextBox.Text, _ "", "", TextToDisplay:=oObjForm.dspTextBox.Text) Set oObjForm = Nothing Next webIndex 'End With 'Write the hyperlink ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _ oObjForm.webaddTextBox.Text, SubAddress:="", ScreenTip:="", TextToDisplay:= _ oObjForm.dspTextBox.Text 'Report MsgBox "Data you entered has been tranfered to the table." & vbCr + vbCr _ & "You can edit, add or delete information to the defined table as required", _ vbInformation + vbOKOnly, "DATA TRANSFER COMPLETE" CleanUp: Set oTbl = Nothing Set oCol = Nothing Unload oFrmInput Set oFrmInput = Nothing Exit Sub Err_NoRecords: MsgBox "You didn't provide any Links." & vbCr + vbCr _ & "You can edit and add information to the basic table if required", _ vbInformation + vbOKOnly, "NO DATA PROVIDED" Resume CleanUp End Sub I hope you can help me figure this thing out. Thank you, Michael |
#8
|
|||
|
|||
Michael,
Again, hard to offer much without your form. However look at your code that works for inserting a stand alone hyperlink. Notice the first parameter you define is the "anchor" as "Selection.Range." Now look at your main code: .Cell(webIndex + 1, 3).Range.End -1 = ActiveDocument.Hyperlinks.Add(oObjForm.webaddTextB ox.Text, _ "", "", TextToDisplay:=oObjForm.dspTextBox.Text) That is a real dog's breakfast ;-). oObjForm.webaddTextBox.Text is "address text" it is not an anchor. First you need to get your anchor. Ok, it is clear that you want the anchor to be at the end of a defined cell. Take a look at this and see if it doesn't help you sort it out. Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oTbl As Word.Table Dim lngIndex As Long Dim oRngAnchor As Range Set oTbl = ActiveDocument.Tables(1) For lngIndex = 1 To oTbl.Rows.Count Set oRngAnchor = oTbl.Cell(lngIndex, 1).Range oRngAnchor.Collapse wdCollapseEnd oRngAnchor.End = oRngAnchor.End - 1 oRngAnchor.InsertBefore " " oRngAnchor.Collapse wdCollapseEnd ActiveDocument.Hyperlinks.Add oRngAnchor, "http//www.google.com", , "Lets brows Google", "Browse Google" Next lngIndex lbl_Exit: Exit Sub End Sub |
#9
|
|||
|
|||
Greg,
Thank you for the quick reply. I'll try to check the code out hopefully soon but may not be until Monday. I originally had the Anchor set but it didn't like it and for life of me I can't remember the error. Maybe something about end of statement.... It didn't dawn on me the coincidence of the Anchor Again thank you so much for all of your help. Michael |
#10
|
|||
|
|||
Greg,
THANK YOU!! I was able to get it to work. It works just the way I needed. I only need one more piece to my puzzle figured out and all should be well. As can be seen from my full code that I posted earlier, I'm using a Collection for the hyperlink data. Is there a way to check how many entries are in a Collection? If so, how would I go about getting that information? What I'm needing is to be able to add "&" in between the hyperlinks when there is more than one hyperlink. I know I can use an If statement to determine if the collection has more than 1 entry, to do one process and if not then do another process. But other than that I'm not sure what the best approach would be to accomplish this. Would it be if there's more than one entry to just put the "&" after all of the links and then have the code delete the last "&". Or to get an accurate count some how and only place "&" between the links. Thank you, Michael |
#11
|
|||
|
|||
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oCol As New Collection oCol.Add "one" oCol.Add "two" oCol.Add "three" MsgBox oCol.Count End Sub |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to copy userform text and formfield contents to outlook? | odin | Word VBA | 7 | 09-09-2014 11:56 AM |
Is it possible to take an input from a UserForm in one document to a UserForm in a do | BoringDavid | Word VBA | 5 | 05-09-2014 09:08 AM |
cross reference hyperlinks to text box | nothing_kills | Word | 4 | 11-25-2013 09:21 AM |
Inserting Text and Hyperlinks | DrDOS | Excel | 2 | 03-21-2012 03:53 AM |
Checkbox on Userform result in Text in Word | Dolfie_twee | Word VBA | 1 | 06-22-2010 07:54 AM |