Share via

How to create a dropdown result add text in a plain text box.

Anonymous
2024-09-16T02:01:54+00:00

I am going insane trying to figure this out. I am familiar with code but am having difficulty getting my document to work. I want to add a drop down that lets the user select what language was used, and then a corresponding plain text field automatically adds text based off the selection in the drop down. I have tried a million different ways but have hit walls. Basically,

  1. User is asked what language
  2. User selects either "English" or "Other than English- Specify"
  3. Text box either reflects "English or "Other than English- Specify" and another text box allowing the user to input the language used. I do not want to see the second text box if "English" is selected.

HELP PLEASE!!!

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Anonymous
    2024-09-17T11:52:01+00:00

    I reviewed the documentation for Word event listeners, and it seems that Microsoft does not provide an event that triggers 'when a dropdown menu option is clicked.'

    Based on my limited VBA knowledge, it appears that the only option available is to use the ContentControlOnExit event. When the user leaves the dropdown menu control, VBA considers the value of the dropdown menu to be correctly stored in memory, thus triggering the code logic within the event—at least, this is my understanding. Until Microsoft provides us with more event listeners, this is the best approach I can think of.

    If my code has been helpful to you or sparked some programming ideas, please select 'Yes' under any response that was helpful.

    It will make it easier for other users encountering the same query to find this thread through search engines. Thank you for your cooperation.

    Have a nice day!

    Thomas

    2 people found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-09-23T02:29:29+00:00

    Hi Gary,

    I noticed that in my reply there was a prompt saying "1 person found this reply helpful," was this you?

    I suspect that you might not have been logged into your account at the time, as typically replies marked as helpful by the OP are highlighted.

    Please ensure you are logged into the account that posted the thread before marking it again. Thank you very much for your cooperation. If you have any other issues in the future, feel free to post in the community, and I will do my utmost to help you resolve them.

    Thomas

    0 comments No comments
  2. Anonymous
    2024-09-20T11:19:42+00:00

    Thank you very much!!!

    0 comments No comments
  3. Anonymous
    2024-09-16T13:04:53+00:00

    I am beginning to think I may have done something to my Word app. This did not work, it's like Word is missing critical functions or templates. I need to delete it and re-install. Once I do that, I will return to let you know if it worked. I even tried the document you sent via PM, but it also did not work. I am pretty sure I did everything correctly when creating it, but the fact that the example you sent didn't work either is what leads me to think this.

    **EDIT**

    I re-installed the software, and it worked. I do have a follow up question: Can the text box that pops up appear when the User hits tab? As of right now it only pops up when someone clicks elsewhere

    0 comments No comments
  4. Anonymous
    2024-09-16T08:46:59+00:00

    Hi Gary Freis,

    Welcome to the Microsoft Community.

    Since you've mentioned that you're familiar with coding, I will skip the basic steps of how to create a dropdown menu and go straight to sharing the logic of the code with you.

    Here is the code:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) 
    
        'Make sure the title of the Drop-down list is "LanguageDropdown" 
    
        If ContentControl.Title = "LanguageDropdown" Then 
    
            ' delete the text box already exit 
    
            Dim shp As Shape 
    
            For Each shp In ActiveDocument.Shapes 
    
                If shp.Type = msoTextBox Then 
    
                    shp.Delete 
    
                End If 
    
            Next shp 
    
            ' if select non-english then insert a textbox 
    
            If ContentControl.Range.Text = "Other than English- Specify" Then 
    
                Set shp = ActiveDocument.Shapes.AddTextbox( _ 
    
                    Orientation:=msoTextOrientationHorizontal, _ 
    
                    Left:=100, Top:=ContentControl.Range.Information(wdVerticalPositionRelativeToPage) + 20, _ 
    
                    Width:=300, Height:=50) 
    
                shp.TextFrame.TextRange.Text = "Specify your language" 
    
            End If 
    
        End If 
    
    End Sub 
    

    The idea is this: we can use the Document_ContentControlOnExit event listener to monitor when the dropdown control is exited. First, we precisely locate the specific dropdown menu using ContentControl.Title. To avoid creating a new text box every time the "Other than English- Specify" option is selected, I first clear any existing text boxes. Then, the code inserts the new text box.

    I have also shared the Word macro document that I created for testing purposes with you via a private message, so please check it if necessary.

    I hope the code and explanation above are helpful to you.

    Best Regards,

    Thomas C - MSFT | Microsoft Community Support Specialist

    0 comments No comments