Macro to change a paragraph of text depending on a drop-down choic

N

Nevets

I would like to create a macro to be used on exit from a drop down field such
that depending on the choice selected in the drop down, the next paragraph of
text is different, and the text is automatically entered on exit from the
drop-down.
The drop-down will have 4 options, "Choose One", as the default option, and
(for example), "XXXX", "YYYY", and "ZZZZ" are the other three.
If "Choose One" is left as the selection, then the next paragraph of text
would be left blank. If "XXXX" is chosen, then the next paragraph would be
"Xxxx xxx xxxx xxxxx...", and so on.
On a related note, is it possible to add some lines to the macro such that
if someone tabs out of the drop-down field without changing from the default
"Choose One" option, a message box appears telling them to go back and make a
selection?
 
N

NZ VBA Developer

Nevets,

The following is not pretty - needs error handling around things like
ensuring the dropdown is a dropdown and the bookmark exists - but it works.
Also, I'd probably use an AutoText entry with the bookmark included so as to
avoid using the Selection object, but at least it will give you someplace to
start.


Sub InsertNextPara()
Dim DDSelection As Integer
DDSelection = ActiveDocument.FormFields("Dropdown1").DropDown.Value
Select Case DDSelection
Case 2
ActiveDocument.Unprotect
ActiveDocument.Bookmarks("NextPara").Select
With Selection
.Expand wdParagraph
.End = .End - 1
.Range.Text = "This is the text for XXXX."
End With
ActiveDocument.Protect wdAllowOnlyFormFields, True
Case 3
ActiveDocument.Unprotect
ActiveDocument.Bookmarks("NextPara").Select
With Selection
.Expand wdParagraph
.End = .End - 1
.Range.Text = "This is the text for YYYY."
End With
ActiveDocument.Protect wdAllowOnlyFormFields, True
Case 4
ActiveDocument.Unprotect
ActiveDocument.Bookmarks("NextPara").Select
With Selection
.Expand wdParagraph
.End = .End - 1
.Range.Text = "This is the text for ZZZZ."
End With
ActiveDocument.Protect wdAllowOnlyFormFields, True
Case Else
MsgBox "You must select a value.", vbCritical, "Dropdown Error"
ActiveDocument.FormFields("Dropdown1").Select
End Select
End Sub

I'm assuming you know how to set up the formfield to run the macro on exit.

Only problem: I can't seem to get the focus to remain with the dropdown
after clearing the message box, even if I try selecting it twice.
--
Cheers!
The Kiwi Koder

Please note: Uninvited email contact will be marked as SPAM and ignored -
unless you want to hire me. ;-)
 
N

Nevets

Struggling to get your suggestion to work, and as you will probably tell from
this reply, I'm not the most proficient at VBA.
I tried to test your method in a simple form, with only 1 dropdown, named
Dropdown1, and the four options suggested. I copied and pasted your macro
into the VBA screen, then chose the InsertNextPara Macro to run on exit from
the dropdown. When I try to run it, the "Case Else" option works, but if I
select XXXX in the dropdown, I get the following error message:
"Run Time Error '5941": The requested member of the collection does not
exist."
If I hit "Debug", the line "ActiveDocument.Bookmarks("NextPara").Select"
under "Case 2" is highlighted. The other options (YYYY or ZZZZ) result in
the same error, and the same line in the corresponding Case being highlighted
if I hit "Debug".
I don't know if this has something to do with your suggestion to "use an
AutoText entry with the bookmark included so as to avoid using the Selection
object", because I don't have any idea what a Selection object is in the
first place!!
I have no idea how to fix this issue. Can you help?
 
N

NZ VBA Developer

I think maybe I missed something in my post. The code also assumes that
there's a bookmark called 'NextPara' at the location where you want to insert
the text. If you send me an email (my address is in my profile) I'll send you
my test template - assuming I've still got it somewhere. I'll also explain my
comment about the Selection object; too hard to go into it here.
--
Cheers!
The Kiwi Koder

Please note: Uninvited email contact will be marked as SPAM and ignored -
unless you want to hire me. ;-)
 

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