Populate options depending on Dropdown selection

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Populate options depending on Dropdown selection

Post by RaudelJr »

Hi,

I'm attaching a sample Word Document macro enabled.

I'm attempting to give the worker the ability to journal different options depending on a dropdown selection.
Sample.dotm
The Idea is as follows:
---------------------------------------------------------------------------
For Options to display below the paragraph below:

"Client Choice:_Dropdown_" (Options are - Self-Service (Online), Help On-Demand, CCC, County Appointment)

>>> Here only the item needed for such selection should display depending on the selection.
---------------------------------------------------------------------------

I was able to have irrelevant items removed, but then it's impossible to get them back if the user selected the wrong dropdown option.

Maybe there is a way to simply populate the paragraphs as built in the document, (along with the legacy form fields).
Creating them, would be much simpler then removing them, because they would need to be created again if the user switches the dropdown selection.

I hope I make sense,

The attached file might clarify any questions.

Thank you in advance.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Populate options depending on Dropdown selection

Post by HansV »

In the attached version, I have turned on 'Calculate on Exit' for the dropdown, cleared the On Exit macro, and used IF fields to hide/show the parts of text.
Sample.dotm
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Populate options depending on Dropdown selection

Post by RaudelJr »

HansV wrote:In the attached version, I have turned on 'Calculate on Exit' for the dropdown, cleared the On Exit macro, and used IF fields to hide/show the parts of text.
Sample.dotm
Hi HansV,
Thank you for looking into this.

Where is the code for the If Fields? I can't seem to find it.

There is also an other item that is necessary. After a selection is chosen, the text that appears needs to include the Legacy Form Fields along with the regular text.

I'm including image samples.
SelfService.PNG
OnDemand.PNG
CCC.PNG
I like the Hide/Show Text approach you are recommending. :evilgrin:

As for my macro, I thought I had my Sub UpdateOptions() End Sub macro at least deleting text correctly, but it seems to actually not be working at all as it is giving a "This is not a valid selection" error, though I did setup the bookmarks properly. :sad:

Is there away to hid the text with the Form Fields, and then show the text with Form Fields, depending on the selection?

Raudel
You do not have the required permissions to view the files attached to this post.

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Populate options depending on Dropdown selection

Post by RaudelJr »

Last image.
CountyAppt.PNG
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Populate options depending on Dropdown selection

Post by HansV »

You can view the field codes by unprotecting the document or template and pressing Alt+F9.
After you're done, press Alt+F9 to hide the field codes.

However, my suggestion won't work since form fields inside an IF field aren't displayed, apparently.

This is beyond my knowledge of Word, sorry about that. I hope that one of our Word experts can help you.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Populate options depending on Dropdown selection

Post by RaudelJr »

HansV wrote:You can view the field codes by unprotecting the document or template and pressing Alt+F9.
After you're done, press Alt+F9 to hide the field codes.

However, my suggestion won't work since form fields inside an IF field aren't displayed, apparently.

This is beyond my knowledge of Word, sorry about that. I hope that one of our Word experts can help you.
Thank you Hansv, I was able to see view the field codes.

Is there a way to do this using VBA?
Maybe creating the entire text with legacy forms details coded based on the dropdown selection?

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Populate options depending on Dropdown selection

Post by HansV »

Again, I hope that someone else can help you.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Populate options depending on Dropdown selection

Post by RaudelJr »

Thanks again HansV for your continued help and support.

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Populate options depending on Dropdown selection

Post by RaudelJr »

Hi,

I was able to make it work with the following Code using, Selection.Font.Hidden = True or Selection.Font.Hidden = False, accordingly.
Using Hidden Fonts the user won't lose any edits to the hidden fields if they mistakenly select a different dropdown option.

Code: Select all

Sub UpdateOptions()
Dim bProtected As Boolean
    'Unprotect the file
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        bProtected = True
        ActiveDocument.Unprotect Password:=""
    End If
' ActiveDocument.Bookmarks(Index:=strBookmark).Delete
'Self-Service (Online)
'Help On-Demand
'Contact Covered California
'County Appointment
    Select Case ActiveDocument.FormFields("ClientChoiceDropdown").Result
            Case "   "
            Range(Me.Bookmarks("SelfServiceStart").Start, Me.Bookmarks("SelfServiceEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("HelpOnDemandStart").Start, Me.Bookmarks("HelpOnDemandEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("CCCStart").Start, Me.Bookmarks("CCCEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("CountyStart").Start, Me.Bookmarks("CountyEnd").End).Select
            Selection.Font.Hidden = True
        Case "Self-Service (Online)"
            Range(Me.Bookmarks("SelfServiceStart").Start, Me.Bookmarks("SelfServiceEnd").End).Select
            Selection.Font.Hidden = False
            Range(Me.Bookmarks("HelpOnDemandStart").Start, Me.Bookmarks("HelpOnDemandEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("CCCStart").Start, Me.Bookmarks("CCCEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("CountyStart").Start, Me.Bookmarks("CountyEnd").End).Select
            Selection.Font.Hidden = True
            ActiveDocument.Bookmarks("SelfServiceDropdown").Select
            SendKeys "%{down}"  'Displays choices in drop-down field
        Case "Help On-Demand"
            Range(Me.Bookmarks("SelfServiceStart").Start, Me.Bookmarks("SelfServiceEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("HelpOnDemandStart").Start, Me.Bookmarks("HelpOnDemandEnd").End).Select
            Selection.Font.Hidden = False
            Range(Me.Bookmarks("CCCStart").Start, Me.Bookmarks("CCCEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("CountyStart").Start, Me.Bookmarks("CountyEnd").End).Select
            Selection.Font.Hidden = True
            ActiveDocument.Bookmarks("HelpOnDemandDropdown").Select
            SendKeys "%{down}"  'Displays choices in drop-down field
        Case "CCC"
            Range(Me.Bookmarks("SelfServiceStart").Start, Me.Bookmarks("SelfServiceEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("HelpOnDemandStart").Start, Me.Bookmarks("HelpOnDemandEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("CCCStart").Start, Me.Bookmarks("CCCEnd").End).Select
            Selection.Font.Hidden = False
            Range(Me.Bookmarks("CountyStart").Start, Me.Bookmarks("CountyEnd").End).Select
            Selection.Font.Hidden = True
            ActiveDocument.Bookmarks("CCCDropdown").Select
            SendKeys "%{down}"  'Displays choices in drop-down field
        Case "County Appointment"
            Range(Me.Bookmarks("SelfServiceStart").Start, Me.Bookmarks("SelfServiceEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("HelpOnDemandStart").Start, Me.Bookmarks("HelpOnDemandEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("CCCStart").Start, Me.Bookmarks("CCCEnd").End).Select
            Selection.Font.Hidden = True
            Range(Me.Bookmarks("CountyStart").Start, Me.Bookmarks("CountyEnd").End).Select
            Selection.Font.Hidden = False
            ActiveDocument.Bookmarks("CountyDropdown").Select
            SendKeys "%{down}"  'Displays choices in drop-down field
    End Select
 If bProtected = True Then
        ActiveDocument.Protect _
                Type:=wdAllowOnlyFormFields, _
                NoReset:=True, _
                Password:=""
    End If
End Sub
But I got a problem. If I right click open the template, everything works, but if I double click on the template, a new document is opened and I get an error:
"This command is not available because no document is open."
No Document is open.PNG
I'm not sure what to do so avoid this error.

Thanks for the help in advance.

Template Word File Attached:
HideUnhide Dropdown Dependent.dotm
Raudel
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Populate options depending on Dropdown selection

Post by HansV »

1) The code should be in a standard module, not in the ThisDocument module.
2) Since the code is now in a standard module, use ActiveDocument.Range instead of Range.
3) You shouldn't use Me - that refers to the template. Use ActiveDocument instead.
4) You hadn't named the dropdowns in the text corresponding to the options (HelpOnDemandDropDown etc.)

See the attached version.
Sample.dotm
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Populate options depending on Dropdown selection

Post by RaudelJr »

HansV wrote:1) The code should be in a standard module, not in the ThisDocument module.
2) Since the code is now in a standard module, use ActiveDocument.Range instead of Range.
3) You shouldn't use Me - that refers to the template. Use ActiveDocument instead.
4) You hadn't named the dropdowns in the text corresponding to the options (HelpOnDemandDropDown etc.)

See the attached version.
Sample.dotm

Hi HansV,

Thank you for modifications to the code.

I was able to apply it to our original document and it worked perfectly.

I also added an AutoOpen Macro code to ensure the Client Choice is reset every time to blank choice:

Code: Select all

Sub AutoOpen()
    Dim bProtected As Boolean
    'Unprotect the file
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        bProtected = True
        ActiveDocument.Unprotect Password:=""
    End If
            
            ActiveDocument.Range(ActiveDocument.Bookmarks("SelfServiceStart").Start, ActiveDocument.Bookmarks("SelfServiceEnd").End).Font.Hidden = True
            ActiveDocument.Range(ActiveDocument.Bookmarks("HelpOnDemandStart").Start, ActiveDocument.Bookmarks("HelpOnDemandEnd").End).Font.Hidden = True
            ActiveDocument.Range(ActiveDocument.Bookmarks("CCCStart").Start, ActiveDocument.Bookmarks("CCCEnd").End).Font.Hidden = True
            ActiveDocument.Range(ActiveDocument.Bookmarks("CountyStart").Start, ActiveDocument.Bookmarks("CountyEnd").End).Font.Hidden = True
            ActiveDocument.Bookmarks("Text1").Select
            ActiveDocument.FormFields("ClientChoiceDropdown").DropDown.Value = 1
        
        If bProtected = True Then
        ActiveDocument.Protect _
            Type:=wdAllowOnlyFormFields, _
            NoReset:=True, _
            Password:=""
    End If
End Sub
Final Code:

Code: Select all

Sub UpdateOptions()
    Dim bProtected As Boolean
    'Unprotect the file
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        bProtected = True
        ActiveDocument.Unprotect Password:=""
    End If

            ActiveDocument.Range(ActiveDocument.Bookmarks("SelfServiceStart").Start, ActiveDocument.Bookmarks("SelfServiceEnd").End).Font.Hidden = True
            ActiveDocument.Range(ActiveDocument.Bookmarks("HelpOnDemandStart").Start, ActiveDocument.Bookmarks("HelpOnDemandEnd").End).Font.Hidden = True
            ActiveDocument.Range(ActiveDocument.Bookmarks("CCCStart").Start, ActiveDocument.Bookmarks("CCCEnd").End).Font.Hidden = True
            ActiveDocument.Range(ActiveDocument.Bookmarks("CountyStart").Start, ActiveDocument.Bookmarks("CountyEnd").End).Font.Hidden = True

    Select Case ActiveDocument.FormFields("ClientChoiceDropdown").Result
        Case "   "
            ActiveDocument.Bookmarks("NoChoice").Select
        Case "Self-Service (Online)"
            ActiveDocument.Range(ActiveDocument.Bookmarks("SelfServiceStart").Start, ActiveDocument.Bookmarks("SelfServiceEnd").End).Font.Hidden = False
            ActiveDocument.Bookmarks("SelfServiceDropdown").Select
            SendKeys "%{down}"  'Displays choices in drop-down field
        Case "Help On-Demand"
            ActiveDocument.Range(ActiveDocument.Bookmarks("HelpOnDemandStart").Start, ActiveDocument.Bookmarks("HelpOnDemandEnd").End).Font.Hidden = False
            ActiveDocument.Bookmarks("HelpOnDemandDropdown").Select
            SendKeys "%{down}"  'Displays choices in drop-down field
        Case "CCC"
            ActiveDocument.Range(ActiveDocument.Bookmarks("CCCStart").Start, ActiveDocument.Bookmarks("CCCEnd").End).Font.Hidden = False
            ActiveDocument.Bookmarks("CCCDropdown").Select
            SendKeys "%{down}"  'Displays choices in drop-down field
        Case "County Appointment"
            ActiveDocument.Range(ActiveDocument.Bookmarks("CountyStart").Start, ActiveDocument.Bookmarks("CountyEnd").End).Font.Hidden = False
            ActiveDocument.Bookmarks("CountyDropdown").Select
            SendKeys "%{down}"  'Displays choices in drop-down field
    End Select
    
    If bProtected = True Then
        ActiveDocument.Protect _
            Type:=wdAllowOnlyFormFields, _
            NoReset:=True, _
            Password:=""
    End If
End Sub
Thank you very much for your help :evilgrin:

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Populate options depending on Dropdown selection

Post by RaudelJr »

Hi,

I added some buttons to the the file to copy content between bookmarks and also to reset form by clearing all fields.

But it seems like adding the Buttons caused a sort of a glitch.

The UpdateUptions() Macro is supposed to select the corresponding dropdown and display the selections.
It does it correctly when the form has no Buttons. But as soon as the Buttons are added, at the end of the UpdateOptions() macro, Word seems to move to a field that is after the intended dropdown.

I"m attaching two files,

One without buttons, UpdateOptions() macro working as expected.
DropdownDependentNoButtons.dotm
(If you were to edit this file and add a command button, and do nothing more, it will begin having the problem)


One with buttons, UpdateOptions() macro affected by the presence of the buttons.
DropdownDependentWithButtons.dotm
Not sure how to resolve the weird behavior.

Thanks in advance for your help.

Raudel
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Populate options depending on Dropdown selection

Post by HansV »

On my PC, it looks like the screen is not redrawn correctly. After switching to another window, then back again, everything looks OK. I don't know whether this is a bug and if there is a workaround or solution.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Populate options depending on Dropdown selection

Post by RaudelJr »

HansV wrote:On my PC, it looks like the screen is not redrawn correctly. After switching to another window, then back again, everything looks OK. I don't know whether this is a bug and if there is a workaround or solution.
I tried it too just now. When I return to the word document after switching windows, the dropdown that should be selected is not. If you Display Hidden Text, you will notice that the wrong field is selected.

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Populate options depending on Dropdown selection

Post by HansV »

Once again, this is out of my league. Sorry!
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Populate options depending on Dropdown selection

Post by RaudelJr »

HansV wrote:Once again, this is out of my league. Sorry!
Thank you HansV. :evilgrin:

I'll continue researching, hope others have encountered this odd behavior.

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Populate options depending on Dropdown selection

Post by RaudelJr »

RaudelJr wrote:
HansV wrote:Once again, this is out of my league. Sorry!
Thank you HansV. :evilgrin:

I'll continue researching, hope others have encountered this odd behavior.
For now, I simply had the Macro select the previous (not intended) dropdown bookmark, and due to the odd behavior, when I do that the intended dropdown is correctly selected.
Go figure. :laugh:

I hope this workaround continues to work.