Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-22-2025, 09:59 PM
CompletelyLost CompletelyLost is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 11 How to auto populate a rich text content control box from a selection in a combo control box Office 2021
Novice
How to auto populate a rich text content control box from a selection in a combo control box
 
Join Date: Apr 2025
Posts: 4
CompletelyLost is on a distinguished road
Default How to auto populate a rich text content control box from a selection in a combo control box

I have Office 365 and that wasn't an option to choose. I am creating a template in Word. I have tried everything I can think of, but I'm not a programmer. All I want to do is have a text box titled "Company" automatically populate based on a dropdown combo content box titled "Contractor", selected value. I think I've tried every option in other threads that I could find. This is the last one and it's not working. I don't know if it makes a difference or not, but the "Company" text box is in the header and the "Contractor" dropdown box is in the main document.

Private Sub Document_ShowTextBasedOnDropdown(ByVal CCtrl As ContentControl, Cancel As Boolean)


Dim i As Long, StrDetails As String
With CCtrl
If .Title = "Company" Then
For i = 1 To .DropdownListEntries.Count
If .DropdownListEntries(i).Text = .Range.Text Then
StrDetails = ActiveDocument.SelectContentControlsByTitle("Contr actor").Item(1).Range.Text
Exit For
End If
End With
End If
End Sub

If I can get this to work, I also have another text box that needs to auto populated based on another text box. So like "Project Name 1" populates "Project Name 2".
Reply With Quote
  #2  
Old 04-22-2025, 11:30 PM
macropod's Avatar
macropod macropod is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 10 How to auto populate a rich text content control box from a selection in a combo control box Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,340
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

For Dependent Text Content Controls, see:
https://www.msofficeforums.com/word-...html#post46903
and, for different elements from a selected item to be output to different content controls, see:
https://www.msofficeforums.com/word-...tml#post120392

Once you've mastered that, a whole new world of possibilities opens up.

For Dependent Dropdown Content Controls, see:
https://www.msofficeforums.com/word-...html#post77762
and, for multiple levels of dependency:
https://www.msofficeforums.com/word-...tml#post132696

For Cascading Dropdown Content Controls, see:
https://www.msofficeforums.com/word-...html#post94603

See also:

Content Control & Formfield Conditional Formatting
https://www.msofficeforums.com/word-...html#post47254

Content Controls and Risk Assessments
https://www.msofficeforums.com/130899-post6.html
https://www.msofficeforums.com/125708-post2.html

Dropdown Content Control Population from Excel
https://www.msofficeforums.com/word-...html#post46287

Creating and Tallying Mutually-exclusive Checkbox Content Controls
https://www.msofficeforums.com/word-...html#post33489
https://www.msofficeforums.com/word-...tml#post107008
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-23-2025, 04:31 AM
gmaxey gmaxey is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 10 How to auto populate a rich text content control box from a selection in a combo control box Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Quote:
Originally Posted by CompletelyLost View Post
I have Office 365 and that wasn't an option to choose. I am creating a template in Word. I have tried everything I can think of, but I'm not a programmer. All I want to do is have a text box titled "Company" automatically populate based on a dropdown combo content box titled "Contractor", selected value. I think I've tried every option in other threads that I could find. This is the last one and it's not working. I don't know if it makes a difference or not, but the "Company" text box is in the header and the "Contractor" dropdown box is in the main document.

Private Sub Document_ShowTextBasedOnDropdown(ByVal CCtrl As ContentControl, Cancel As Boolean)
Dim i As Long, StrDetails As String
With CCtrl
If .Title = "Company" Then
For i = 1 To .DropdownListEntries.Count
If .DropdownListEntries(i).Text = .Range.Text Then
StrDetails = ActiveDocument.SelectContentControlsByTitle("Contr actor").Item(1).Range.Text
Exit For
End If
End With
End If
End Sub

If I can get this to work, I also have another text box that needs to auto populated based on another text box. So like "Project Name 1" populates "Project Name 2".

Lost,


You will learn a lot of information reading all of Paul's links and if I have read them all, it has been awhile. That aside, lets look at your code.

1. As your Private Sub Document_ShowTextBasedOnDropdown(ByVal CCtrl As ContentControl, Cancel As Boolean) has parameters (CCrl and Cancel) it appears that you have tried to create your own event that you want to fire when you change the dropdown. It doesn't work that way. The available (or readily available) events you can work with are located in the ThisDocument Module, Document. See events. For your case, you could use the ContentControl_OnExit event.

2. Your code as written does not compile. You have a spurious (not required End If) following your End With and you are missing a required Next i (or just Next)


3. Your stated goal is to populate a text content control titled "Company" based on a combo box control titled "Contractor." However, in your code, it appears that you are trying to iterate over a list of dropdown entries of Company.


Here is a very basic macro that might work for you:

Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim oCC As ContentControl
  Select Case ContentControl.Title
    Case "Contractor"
      Set oCC = ActiveDocument.SelectContentControlsByTitle("Company").Item(1)
      Select Case ContentControl.Range.Text
        Case "Contractor A": oCC.Range.Text = "Acme Hardware"
        Case "Contractor B": oCC.Range.Text = "Coswell Cogs"
        'Etc.
      End Select
  End Select
End Sub


Now, are you sure that your users will be allow to use macros? I ask because if you plan to share your template with other users outside of your organization they may not! There are a lot of very anal IT people out there that block macros from running on company computers.


For this particular requirement, you don't even need a macro. You can achieve this goal by using a pair of mapped content controls. For example:


1 .In your dropdown, define your list like this:

Display Name Value
Contractor A Acme Hardware
Contractor B Coswell Cogs
Contractor C Slate Rock and Gravel
etc. etc.


2. Create a simple .txt file containing the following content:
<?xml version="1.0"?><Root xmlns="http://TheAnchorage/SimpleXMLPart"><Contractor></Contractor></Root>


3. Using the Developer Tab XML Mapping Pane, add a new part. The new part will be the simple .txt file described above.


4. Map both the Contractor and Company content controls in your document to the "Contractor" node of the CustomXMLPart.


5. Viola
Attached Images
File Type: jpg Events.jpg (142.1 KB, 18 views)
Attached Files
File Type: docm Demo Document.docm (40.6 KB, 0 views)
File Type: txt SimpleXML.txt (103 Bytes, 0 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 04-23-2025, 10:19 AM
CompletelyLost CompletelyLost is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 11 How to auto populate a rich text content control box from a selection in a combo control box Office 2021
Novice
How to auto populate a rich text content control box from a selection in a combo control box
 
Join Date: Apr 2025
Posts: 4
CompletelyLost is on a distinguished road
Default

Thanks Greg. Here's my problem. I already have a macro with Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean), so when I try to start another one, it tells me it's already in use. I've tried merging them together and that didn't work. It just messed up the first one. Did I mention I'm not a programmer and have no idea what I'm doing LOL. This is said current macro using the ContentControlOnExit:

Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Dim oDLE As ContentControlListEntry
With CCtrl
If .Type = wdContentControlComboBox Then
For Each oDLE In .DropdownListEntries
If oDLE.Text = .Range.Text Then
.Range.Text = oDLE.Value
Exit For
End If
Next
End If
End With
End Sub

That said, I was trying to avoid having to spell out exactly the contractor name that is selected in the drop down as there is no difference in Display Name and Value as you show by using the Case function. The above macro is being used to select the Value instead of the Display name in dropdowns that are different. I just want if Company A is selected from the Contractor dropdown, I want my Company text box to show Company A. Can I add what I want to the above macro somehow? If this is not possible, I will have to type out all of the companies as you have suggested by using the Case function, but how do I merge it with the above so I don't get an error?

This will be an internal document only. It's basically a contract that I'm trying to make idiot proof LOL.
Reply With Quote
  #5  
Old 04-23-2025, 10:44 AM
gmaxey gmaxey is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 10 How to auto populate a rich text content control box from a selection in a combo control box Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

If you have two or more CCs you want to handle with a Change Event, then use something like this:

Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Dim oDLE As ContentControlListEntry
  With CCtrl
    Select Case .Title
    Case "Demo1"
      For Each oDLE In .DropdownListEntries
        If oDLE.Text = .Range.Text Then
          .Range.Text = oDLE.Value
          Exit For
        End If
      Next oDLE
    Case "Contractor"
      For Each oDLE In .DropdownListEntries
        If oDLE.Text = .Range.Text Then
          .Range.Text = oDLE.Value
          ActiveDocument.SelectContentControlsByTitle("Company").Item(1).Range.Text = oDLE.Value
          Exit For
        End If
      Next oDLE
    End Select
  End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 04-23-2025, 12:55 PM
Charles Kenyon Charles Kenyon is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 11 How to auto populate a rich text content control box from a selection in a combo control box Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,453
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Digression:
Why a Rich Text CC?
Because they catch tabs, they get in the way of making forms idiot-proof, in my opinion. There can be very good reasons to use Rich Text, but often there are not.
Reply With Quote
  #7  
Old 04-23-2025, 02:22 PM
CompletelyLost CompletelyLost is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 11 How to auto populate a rich text content control box from a selection in a combo control box Office 2021
Novice
How to auto populate a rich text content control box from a selection in a combo control box
 
Join Date: Apr 2025
Posts: 4
CompletelyLost is on a distinguished road
Default

Greg,

I tried the code and based on what you had, I assumed I had to put the "Title" of every box that I was associating the original code with as a Case since you used the title Demo1. Now I get a Run-time error '6124' that says I'm not allowed to edit this selection because it is protected. Yes, I enabled protection to Form Only, to test for how a user will interact. No, the box locking contents from being edited is not checked. This is what I did:

Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Dim oDLE As ContentControlListEntry
  With CCtrl
    Select Case .Title
    Case "Address"
      For Each oDLE In .DropdownListEntries
        If oDLE.Text = .Range.Text Then
          .Range.Text = oDLE.Value
          Exit For
        End If
      Next oDLE
    Case "City"
      For Each oDLE In .DropdownListEntries
        If oDLE.Text = .Range.Text Then
          .Range.Text = oDLE.Value
          Exit For
        End If
      Next oDLE
   Case "Phone"
      For Each oDLE In .DropdownListEntries
        If oDLE.Text = .Range.Text Then
          .Range.Text = oDLE.Value
          Exit For
        End If
      Next oDLE
   Case "Fax"
      For Each oDLE In .DropdownListEntries
        If oDLE.Text = .Range.Text Then
          .Range.Text = oDLE.Value
          Exit For
        End If
      Next oDLE
   Case "License"
      For Each oDLE In .DropdownListEntries
        If oDLE.Text = .Range.Text Then
          .Range.Text = oDLE.Value
          Exit For
        End If
      Next oDLE
   Case "Contractor"
      For Each oDLE In .DropdownListEntries
        If oDLE.Text = .Range.Text Then
          .Range.Text = oDLE.Value
          ActiveDocument.SelectContentControlsByTitle("Company").Item(1).Range.Text = oDLE.Value
          Exit For
        End If
      Next oDLE
    End Select
  End With
End Sub
All other dropdown boxes have the same Display Name and Value. Suggestions?

Last edited by macropod; 04-23-2025 at 04:29 PM. Reason: Added code tags - again
Reply With Quote
  #8  
Old 04-23-2025, 02:27 PM
CompletelyLost CompletelyLost is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 11 How to auto populate a rich text content control box from a selection in a combo control box Office 2021
Novice
How to auto populate a rich text content control box from a selection in a combo control box
 
Join Date: Apr 2025
Posts: 4
CompletelyLost is on a distinguished road
Default

Charles,

Honestly, I never really thought about the difference between Rich Text and Plain Text, but now that you mention it, I did notice that my tab sticks. I changed one to test. It works now! Thanks for the suggestion! I will change them all.
Reply With Quote
  #9  
Old 04-24-2025, 09:18 AM
gmaxey gmaxey is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 10 How to auto populate a rich text content control box from a selection in a combo control box Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Content Controls are not form fields so they will not work when protected for Filling In Form. You would have to use No Changes (Read Only) and apply editors to the content controls.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 04-24-2025, 04:42 PM
macropod's Avatar
macropod macropod is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 10 How to auto populate a rich text content control box from a selection in a combo control box Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,340
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 gmaxey View Post
Content Controls are not form fields so they will not work when protected for Filling In Form. You would have to use No Changes (Read Only) and apply editors to the content controls.
Actually, they do work in documents with 'Filling In Forms' protection applied; they just don't work the same way as formfields.

Where they don't work well is in documents that also contain formfields.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 04-25-2025, 04:58 AM
gmaxey gmaxey is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 10 How to auto populate a rich text content control box from a selection in a combo control box Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Paul, of course you are correct (at least partially in my opinion). My statement was certainly misleading and incorrect. CCs do work in documents restricted for filling in forms. What doesn't work is the the line of code that I provided to write a value to the "Company" CC. You can't do that when the form is restricted for filling in forms. You would first have to unprotect the document.

Code:
Case "Contractor"
      For Each oDLE In .DropdownListEntries
        If oDLE.Text = .Range.Text Then
          .Range.Text = oDLE.Value
          ActiveDocument.Unprotect 'plus password if used.
          ActiveDocument.SelectContentControlsByTitle("Company").Item(1).Range.Text = oDLE.Value
          ActiveDocument.Protect wdAllowOnlyFormFields, False 'Plus password if ture
          Exit For
        End If
      Next oDLE

Now, where our opinions differ is "Where they don't work well is in documents that also contain formfields." Personally, I have never encountered a difficulty.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #12  
Old 04-25-2025, 03:44 PM
macropod's Avatar
macropod macropod is offline How to auto populate a rich text content control box from a selection in a combo control box Windows 10 How to auto populate a rich text content control box from a selection in a combo control box Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,340
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 gmaxey View Post
CCs do work in documents restricted for filling in forms. What doesn't work is the the line of code that I provided to write a value to the "Company" CC. You can't do that when the form is restricted for filling in forms.
If you take the document in the first link I posted and apply 'Filling in Forms' protection to it, the dependent content control still gets updated without any unprotection being required.
Quote:
Now, where our opinions differ is "Where they don't work well is in documents that also contain formfields." Personally, I have never encountered a difficulty.
If you create a document with a mix of Content Controls and Formfields, what you'll find is that, having tabbed from a Formfield into a Content Control, you can't tab out of the Content Control to the next Formfield.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to auto populate a rich text content control box from a selection in a combo control box Auto populate a text field based on Content Control Dropdown List matthias92701 Word VBA 2 05-02-2019 03:54 PM
Copy Formatted Text in one Rich Text Content Control to another Rich Text Content Control Haygordon Word 1 04-05-2019 05:43 AM
How to auto populate a rich text content control box from a selection in a combo control box Auto populate text box based on drop-down content control and repeat the process via copy paste helenndp Word VBA 2 09-27-2018 11:04 AM
Combo Box Content Control Calculate on selection help ciresuark Word 0 03-06-2015 01:49 PM
How to auto populate a rich text content control box from a selection in a combo control box How do you set rich text in a content control Testor Word VBA 4 07-08-2012 07:55 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:26 AM.


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