![]() |
|
![]() |
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
![]()
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". |
#2
|
||||
|
||||
![]()
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] |
#3
|
|||
|
|||
![]() Quote:
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 |
#4
|
|||
|
|||
![]()
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. |
#5
|
|||
|
|||
![]()
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 |
#6
|
|||
|
|||
![]()
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. |
#7
|
|||
|
|||
![]()
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 Last edited by macropod; 04-23-2025 at 04:29 PM. Reason: Added code tags - again |
#8
|
|||
|
|||
![]()
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. |
#9
|
|||
|
|||
![]()
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.
|
#10
|
||||
|
||||
![]() Quote:
Where they don't work well is in documents that also contain formfields.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
![]()
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. |
#12
|
||||
|
||||
![]() Quote:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |
![]() |
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 |
![]() |
Testor | Word VBA | 4 | 07-08-2012 07:55 AM |