View Single Post
 
Old 04-23-2025, 04:31 AM
gmaxey gmaxey is offline Windows 10 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