Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-11-2014, 09:09 AM
odin odin is offline RichTextContentControl with text and hyperlinks via Userform possible? Windows 7 64bit RichTextContentControl with text and hyperlinks via Userform possible? Office 2010 64bit
Novice
RichTextContentControl with text and hyperlinks via Userform possible?
 
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
  #2  
Old 09-11-2014, 04:28 PM
macropod's Avatar
macropod macropod is offline RichTextContentControl with text and hyperlinks via Userform possible? Windows 7 64bit RichTextContentControl with text and hyperlinks via Userform possible? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by odin View Post
I created a userform with TextBox(s) that will fill some legacy formfields and a Rich Text Content Control via it's Tag.
Bad move. Content controls and formfields should not be used in the same document. They are not designed to work together and using them that way can cause the documents to misbehave.
Quote:
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?
Yes. I can't see what the issue is, unless you haven't figured out how to insert the hyperlink without the user selecting the insertion point, or you want to be able to want the user to be able to select the insertion point while the userform is displayed.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-13-2014, 09:23 AM
odin odin is offline RichTextContentControl with text and hyperlinks via Userform possible? Windows 7 64bit RichTextContentControl with text and hyperlinks via Userform possible? Office 2010 64bit
Novice
RichTextContentControl with text and hyperlinks via Userform possible?
 
Join Date: Sep 2014
Posts: 11
odin is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Bad move. Content controls and formfields should not be used in the same document. They are not designed to work together and using them that way can cause the documents to misbehave.
Yes. I can't see what the issue is, unless you haven't figured out how to insert the hyperlink without the user selecting the insertion point, or you want to be able to want the user to be able to select the insertion point while the userform is displayed.
I can replace the form fields with content controls if having both in one document can cause it to misbehave. Yes I would like the user to be able to pick in the userform where they want the hyperlink to be placed. I'm thinking that I made need to do an array maybe? When they type the display text for the link, it captures the text box contents, then replaces it and adds the display text along with the original content. And the array would have the display text and address stored. Then when completed it inserts the contents into the content control and replaces the hyperlink display text entries with the formed hyperlink? But I don't know how to grab the cursor location, especially in a text box while in a userform.
Reply With Quote
  #4  
Old 09-13-2014, 03:26 PM
macropod's Avatar
macropod macropod is offline RichTextContentControl with text and hyperlinks via Userform possible? Windows 7 64bit RichTextContentControl with text and hyperlinks via Userform possible? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 09-18-2014, 09:25 AM
odin odin is offline RichTextContentControl with text and hyperlinks via Userform possible? Windows 7 64bit RichTextContentControl with text and hyperlinks via Userform possible? Office 2010 64bit
Novice
RichTextContentControl with text and hyperlinks via Userform possible?
 
Join Date: Sep 2014
Posts: 11
odin is on a distinguished road
Default

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
Also Tried this:
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
If I have the following by itself to run as alone it'll make the hyperlink and no errors:
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"
I am at a loss as of now. Any ideas?
Reply With Quote
  #6  
Old 09-18-2014, 10:39 AM
gmaxey gmaxey is offline RichTextContentControl with text and hyperlinks via Userform possible? Windows 7 32bit RichTextContentControl with text and hyperlinks via Userform possible? Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,437
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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"
and claim it works. It won't work as it will result in a compile error expecting End Sub. Now that is obvious. However, I tried:

Code:
.Cell(webIndex + 1, 3).Range.End -1 = ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=oObjForm.webaddTextBox.Text, _
        SubAddress:="", ScreenTip:="", TextToDisplay:=oObjForm.dspTextBox.Text
is not obvious and neither is the other cryptic code segment.

What is your full code?
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 09-18-2014, 12:02 PM
odin odin is offline RichTextContentControl with text and hyperlinks via Userform possible? Windows 7 64bit RichTextContentControl with text and hyperlinks via Userform possible? Office 2010 64bit
Novice
RichTextContentControl with text and hyperlinks via Userform possible?
 
Join Date: Sep 2014
Posts: 11
odin is on a distinguished road
Default

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
The code above though I should mention is just a standard macro not run from anything other than going to Macros and selecting it to run. And it will insert the hyperlink at the current currsor location.

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
Another issue I'm having with the code is that it looks like I can't use ".Range.End -1" statement either to try and insert the link or anything at the end of the existing text in a cell in a table. Per the Word help the ".Range.End" is read only and you're not able to write to it.

I hope you can help me figure this thing out.

Thank you,
Michael
Reply With Quote
  #8  
Old 09-18-2014, 01:34 PM
gmaxey gmaxey is offline RichTextContentControl with text and hyperlinks via Userform possible? Windows 7 32bit RichTextContentControl with text and hyperlinks via Userform possible? Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,437
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 09-18-2014, 02:21 PM
odin odin is offline RichTextContentControl with text and hyperlinks via Userform possible? Windows 7 64bit RichTextContentControl with text and hyperlinks via Userform possible? Office 2010 64bit
Novice
RichTextContentControl with text and hyperlinks via Userform possible?
 
Join Date: Sep 2014
Posts: 11
odin is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 09-24-2014, 12:38 PM
odin odin is offline RichTextContentControl with text and hyperlinks via Userform possible? Windows 7 64bit RichTextContentControl with text and hyperlinks via Userform possible? Office 2010 64bit
Novice
RichTextContentControl with text and hyperlinks via Userform possible?
 
Join Date: Sep 2014
Posts: 11
odin is on a distinguished road
Default

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
Reply With Quote
  #11  
Old 09-24-2014, 12:47 PM
gmaxey gmaxey is offline RichTextContentControl with text and hyperlinks via Userform possible? Windows 7 32bit RichTextContentControl with text and hyperlinks via Userform possible? Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,437
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



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
RichTextContentControl with text and hyperlinks via Userform possible? 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
RichTextContentControl with text and hyperlinks via Userform possible? Inserting Text and Hyperlinks DrDOS Excel 2 03-21-2012 03:53 AM
RichTextContentControl with text and hyperlinks via Userform possible? Checkbox on Userform result in Text in Word Dolfie_twee Word VBA 1 06-22-2010 07:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft