Populate options depending on Dropdown selection (1 Viewer)

RaudelJr

New member
Local time
Today, 12:49
Joined
May 1, 2019
Messages
3
Hi All,

I've been working on a Template that is able to populate different options depending on a Dropdown selection.

If a certain selection is made, below the dropdown, text will display (which includes legacy form fields), but that text will change as soon as another selection is made.

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.

The text to be displayed or hidden is determined by set bookmarks that are easily identified by their descriptive bookmark name.

Code:
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 t


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."

Those that will be working with this template will never save the New File created, but either way, I tried saving the file and the error still appears.

I'm not sure what to do so avoid this error.

I have attached photos of how the document should be with every Dropdown selection under "Client Choice"

I've also attached a photo of the error, and a zip file that contains the Macro Enabled Word Document.

Thank you in advance for your help. :)

Raudel
 

Attachments

  • SelfService.PNG
    SelfService.PNG
    11.9 KB · Views: 142
  • OnDemand.PNG
    OnDemand.PNG
    12.7 KB · Views: 144
  • CCC.PNG
    CCC.PNG
    12 KB · Views: 149
  • CountyAppt.PNG
    CountyAppt.PNG
    13.2 KB · Views: 140
  • HideUnhide Dropdown Dependent.zip
    27.4 KB · Views: 138
Last edited:

RaudelJr

New member
Local time
Today, 12:49
Joined
May 1, 2019
Messages
3
I had reached the maximum attachments on the previous post.

Attached the error image here.

Thanks again.
 

Attachments

  • No Document is open.PNG
    No Document is open.PNG
    5.1 KB · Views: 137

RaudelJr

New member
Local time
Today, 12:49
Joined
May 1, 2019
Messages
3
Changed code as follows and it's fine now.
Thank you all that looked.

Code:
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("Comments").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
 

Users who are viewing this thread

Top Bottom