#1
|
|||
|
|||
Absolute Dependent Dropdown lists
Using content controls, is it possible to create a dropdown that only appears, or becomes live, after a given response to a previous dropdown? For example, if a form filler responds with a yes, then another content control opens with a list of options, but if they respond with a no, the second dropdown doesn't appear or remains inaccessible? If so, how?
|
#2
|
||||
|
||||
Yes, but (aside from the extra code that would be required) the problem with that is in telling Word where the new content control should go. One might use bookmarks, but those are all too easily deleted, expanded or moved by users who don't recognise their presence.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
You can actually make this relatively robust (if complicated) by mapping some CCs to a customXMLPart and playing a shell game with the node text:
In the attached I created a master Yes/No "Question" DDL, a rich text "Container" CC, a temporary Rich Text CC containing a dependent DDL and mapped them to a customXML part. I then added a null node to the CXP. Then I deleted the temporary rich text CC and dependent DDL. Code:
Option Explicit Dim WithEvents oCXPart As CustomXMLPart Sub AutoOpen() On Error Resume Next Set oCXPart = ActiveDocument.CustomXMLParts.SelectByNamespace("http://TheAnchorage.com/XMLPart").Item(1) lbl_Exit: Exit Sub End Sub Private Sub oCXPart_NodeAfterDelete(ByVal OldNode As Office.CustomXMLNode, ByVal OldParentNode As Office.CustomXMLNode, ByVal OldNextSibling As Office.CustomXMLNode, ByVal InUndoRedo As Boolean) 'This event fires when a #text node containing a value is replaced with Null value. If OldParentNode.BaseName = "Question" Then ProcessChange OldNode, True lbl_Exit: Exit Sub End Sub Private Sub oCXPart_NodeAfterInsert(ByVal NewNode As Office.CustomXMLNode, ByVal InUndoRedo As Boolean) 'This event fires when the Null value in a CXPart element node is replaced with a #text node containing a value. ProcessChange NewNode lbl_Exit: Exit Sub End Sub Private Sub oCXPart_NodeAfterReplace(ByVal OldNode As Office.CustomXMLNode, ByVal NewNode As Office.CustomXMLNode, ByVal InUndoRedo As Boolean) 'This event fires when the initial Null value in the node is replaced with a value. ProcessChange NewNode lbl_Exit: Exit Sub End Sub Sub ProcessChange(oNodePassed As Office.CustomXMLNode, Optional bDeadNode As Boolean = False) Dim oNode As CustomXMLNode Dim oCC As ContentControl If bDeadNode Then oCXPart.DocumentElement.ChildNodes(4).Text = oCXPart.DocumentElement.ChildNodes(3).Text Else If oNodePassed.ParentNode.BaseName = "Question" Then Select Case oNodePassed.Text Case Is = "Yes" oCXPart.DocumentElement.ChildNodes(4).Text = oCXPart.DocumentElement.ChildNodes(2).Text Case Else oCXPart.DocumentElement.ChildNodes(4).Text = oCXPart.DocumentElement.ChildNodes(3).Text End Select End If End If lbl_Exit: Exit Sub End Sub |
#4
|
||||
|
||||
Interesting, but the container remains in the document. I'm not sure what advantage that has over simply having a dependent dropdown control whose 'empty' placeholder text consists of, say, a 0-width space.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Paul, how is the container remaining in the document anymore egregious than leaving a bookmark in the document? The idea is to leverage the customXMLPart and its events. However, I take your point, it is over complicated to nest a DDL in a rich text CC.
As a demonstration of concept, I've modified the attached to add a plain text and DDL which is dependent on a master DDL selection. Code:
Option Explicit Dim WithEvents oCXPart As CustomXMLPart Sub AutoOpen() On Error Resume Next Set oCXPart = ActiveDocument.CustomXMLParts.SelectByNamespace("http://TheAnchorage.com/XMLPart").Item(1) lbl_Exit: Exit Sub End Sub Private Sub oCXPart_NodeAfterDelete(ByVal OldNode As Office.CustomXMLNode, ByVal OldParentNode As Office.CustomXMLNode, ByVal OldNextSibling As Office.CustomXMLNode, ByVal InUndoRedo As Boolean) 'This event fires when a #text node containing a value is replaced with Null value. If OldParentNode.BaseName = "Question" Then ProcessChange OldNode, True lbl_Exit: Exit Sub End Sub Private Sub oCXPart_NodeAfterInsert(ByVal NewNode As Office.CustomXMLNode, ByVal InUndoRedo As Boolean) 'This event fires when the Null value in a CXPart element node is replaced with a #text node containing a value. ProcessChange NewNode lbl_Exit: Exit Sub End Sub Private Sub oCXPart_NodeAfterReplace(ByVal OldNode As Office.CustomXMLNode, ByVal NewNode As Office.CustomXMLNode, ByVal InUndoRedo As Boolean) 'This event fires when the initial Null value in the node is replaced with a value. ProcessChange NewNode lbl_Exit: Exit Sub End Sub Sub ProcessChange(oNodePassed As Office.CustomXMLNode, Optional bDeadNode As Boolean = False) Dim oNode As CustomXMLNode Dim oCC_DDL As ContentControl, oCC_PT As ContentControl Set oCC_DDL = ActiveDocument.SelectContentControlsByTag("DepDDL").Item(1) Set oCC_PT = ActiveDocument.SelectContentControlsByTag("fixText").Item(1) If bDeadNode Then With oCC_DDL .LockContents = False .Type = 1 .Range.Text = vbNullString .SetPlaceholderText , , ChrW(8203) .LockContents = True End With With oCC_PT .LockContents = False .Range.Text = vbNullString .LockContents = True End With Else If oNodePassed.ParentNode.BaseName = "Question" Then Select Case oNodePassed.Text Case Is = "Yes" With oCC_DDL .SetPlaceholderText , , "Choose item" .Type = 4 .LockContents = False .Range.Select End With With oCC_PT .LockContents = False .Range.Text = "Please select from menu:" .LockContents = True End With Case Else With oCC_DDL .LockContents = False .Type = 1 .Range.Text = vbNullString .SetPlaceholderText , , ChrW(8203) .LockContents = True End With With oCC_PT .LockContents = False .Range.Text = vbNullString .LockContents = True End With End Select End If End If lbl_Exit: Exit Sub End Sub |
#6
|
||||
|
||||
Quote:
Quote:
The real issue, though is whether anything more substantial than a bookmark should exist in the document when the dependent dropdown control isn't needed.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
#dropdown#dependent |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Content Controls - Dependent Dropdown | someazguy | Word VBA | 14 | 02-05-2023 08:01 PM |
Multiple dependent dropdown lists in table with add new row option | jeweldarby | Word VBA | 6 | 05-21-2022 04:37 PM |
Dependent dropdown and repeat section | NLJ | Word VBA | 3 | 01-12-2020 05:00 PM |
Dropdown dependent text | JakeLRL | Word VBA | 7 | 04-07-2016 08:26 AM |
Dependent drop down lists | bobbych | Word | 1 | 02-08-2013 04:38 PM |