Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nevets - 18 Dec 2007 17:11 GMT
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?
NZ VBA Developer - 18 Dec 2007 22:15 GMT
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.
Signature

Cheers!
The Kiwi Koder

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

> 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
[quoted text clipped - 9 lines]
> "Choose One" option, a message box appears telling them to go back and make a
> selection?
Nevets - 04 Jan 2008 16:09 GMT
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?

> Nevets,
>
[quoted text clipped - 59 lines]
> > "Choose One" option, a message box appears telling them to go back and make a
> > selection?
NZ VBA Developer - 07 Jan 2008 23:31 GMT
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.
Signature

Cheers!
The Kiwi Koder

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

> Struggling to get your suggestion to work, and as you will probably tell from
> this reply, I'm not the most proficient at VBA.
[quoted text clipped - 78 lines]
> > > "Choose One" option, a message box appears telling them to go back and make a
> > > selection?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.