Hide questions in a questionnaire - macro needed?

C

chris

I am trying to understand if it is possible to create a questionnaire where
questions are hidden until a certain answer is given.

E.g. Answer 'No' to question 1, then question 1b opens up that asks a
further question.

I presume some sort of macro would be needed for this? Any help much
appreciated.
 
C

chris

Greg - thanks. That's almost it but I want the question to be hidden and only
expand when a certain answer is given (rather than a button to double click).
Do you think that's possible or will I have to use excel?
 
G

Greg Maxey

Chris,

It can be done (with difficulty) and AFAIK, you can't do it without
VBA.

You can create a question in the form of:

Why did the chicken cross the road? Dropdown", and designate a correct
answer e.g., "To get to the other side.

You can create a follow up question in the form of:

But did he say why he wanted to get to the other side? "Dropdown", and
save it as an AutoText entry e.g., "FUQ" (no salaciousness intended).
Note: I bookmarked the Dropdown field in the FUQ as "FUA"

You can insert it at as an AutoText entry and the dropdown is fully
functional and it is fully functional if entered as an AutoText field
e.g., { AUTOTEXT "FUQ" } However, if you nest the AutoText field in a
conditional IF field it fails to display properly.

But did he say why he crossed the road? Missing the dropdown
{IF 1 = "1" "{AutoText "FUQ" }"""" Displays only "But did he say why
...." but not the dropdown.

I am stumped by this and perhaps someone smarter than me has solved
this mystery.

So moving on to VBA.

Again, this was a perplexing challenge. It is easy enough to determine
if the correct answer is given and designate the position that the FUQ
should appear. I used a Select Case method where the case evaluated
was the DropDown field selection. I set a bookmark "FUQ" where the
follow up question would appear.

I stayed with the AutoText entry approach as they are easily inserted
using AutoText. Or and least I thought.

Anytime you use bookmarks to show one thing or another with VBA there
is the problem of redefining the bookmark to suit its intended new
content. If the FUQ was inserted after or before the bookmark and then
the user changed his or her mind about their first answer then you
would quickly have a dog's breakfast on your hands.

http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm

explains this very well.

With that in mind came up with the following code:

Sub OnExitDD1()
Dim oFF As FormFields
Dim oRng As Word.Range
Set oFF = ActiveDocument.FormFields
Select Case
oFF("DropDown1").DropDown.ListEntries(oFF("DropDown1").DropDown.Value).Name
Case Is = "To get to the other side"
ActiveDocument.Unprotect
Set oRng = ActiveDocument.Bookmarks("FUQ1").Range
oRng.Text = NormalTemplate.AutoTextEntries("FUQ")
ActiveDocument.Bookmarks.Add "FUQ1", oRng
ActiveDocument.Protect wdAllowOnlyFormFields, True
Case Else
ActiveDocument.Unprotect
Set oRng = ActiveDocument.Bookmarks("FUQ1").Range
oRng.Text = ""
ActiveDocument.Bookmarks.Add "FUQ1", oRng
ActiveDocument.Protect wdAllowOnlyFormFields, True
End Select
End Sub

But did he say why he crossed the road? * FORMDROPDOWN **

For some reason (and again I don't understand why). The physical
formfield in the FUQ was converted to text. I suppose that it is due
to using the .Text property of the range. There doesn't seem to be a
..Content property to use. Again perhaps a more experienced programmer
can offer explanation.

Not to be discouraged, I adapted the code to this:

Sub OnExitDropDown1()
Dim oFF As FormFields
Dim oRng As Word.Range
Set oFF = ActiveDocument.FormFields
Select Case
oFF("DropDown1").DropDown.ListEntries(oFF("DropDown1").DropDown.Value).Name
Case Is = "To get to the other side"
ActiveDocument.Unprotect
Set oRng = ActiveDocument.Bookmarks("FUQ").Range
NormalTemplate.AutoTextEntries("FUQ").Insert oRng
oRng.MoveEndUntil Cset:="?", Count:=wdForward
oRng.MoveEnd wdWord, 2
ActiveDocument.Bookmarks.Add "FUQ", oRng
ActiveDocument.Protect wdAllowOnlyFormFields, True
ActiveDocument.Bookmarks("FUA").Range.FormFields(1).Select
Case Else
ActiveDocument.Unprotect
Set oRng = ActiveDocument.Bookmarks("FUQ").Range
oRng.Text = ""
ActiveDocument.Bookmarks.Add "FUQ", oRng
ActiveDocument.Protect wdAllowOnlyFormFields, True
End Select
End Sub

Using the AutoText.Insert method the autotext is inserted with a fully
functional dropdown field. I thin simply expanded oRng forward until I
found the "?" mark then forward again two words (the space and the
formfield). This now but the entire FUQ within oRng and I could
recreate the bookmark.

While I wish that I could determine a more standard approach it appears
to be working.

Note, when I created the AutoText entry FUQ, I bookmarked the formfield
"FUA." You will see in the code that when the user tabs out of the
dropdown answer field that the selection is moved to FUA if it was
created.

HTH.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top