Drop down field formatting

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Happy New Year everyone

i have a good knowledge of word, but know nothing about using macros. I have
created a form with dropdown fields with 2 or more choices, i would like to
attach a macro (conditional format) to each choice of selection (within the
DD field) that will change current font attributes according to the users
choice. can anyone help?

example typical choices: DD field contains
1. Not selected (Arial)
2. SELECTED (Arial Black)

thanking you in advance for your help with this problem
colin
 
Something like this should do. Set the macro to run on exit from the
dropdown field:

Sub FormatResult()
Dim oFF As FormField
ActiveDocument.Unprotect
Set oFF = Selection.FormFields(1)
Select Case oFF.Result
Case Is = "Selected"
oFF.Range.Font.Name = "Arial Bold"
Case Is = "Not Selected"
oFF.Range.Font.Name = "Arial"
Case Else
'Do Nothing
End Select
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
 
Out of curiosity, why did you elect to use: Case Is="Not Selected" instead
of Case "Not Selected"?

I've always seen Is be used for comparison operators in a Case statement.
Something like Case Is >50.

Also note that Select Case is case-sensitive so if SELECTED is in uppercase
in the dropdown then it needs to be the same in the macro.

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
Beth,

It was just an oversight. I initially started out with

Select Case oFF.Dropdown.Value
Case "Not Selected"

and got a type error

Then I typed in Case Is and the editor automatically changed that to Case Is
=
but I still got the type error. Then I realised that dropdown.value was a
numerica value and changed that to Select Case oFF.Result and just didn't
take the time to change the case statements.
 
thanks for the speedy response

Like i said, i know nothing about macros, i created the macro and attached
it to my dropdown field and the only thing that happened was on exit it would
ask me for the password to unprotect the document, if i cancelled, it
returned an error, if i entered the password, i was back to an unprotected
doc.

1. should this be happening? (i want the doc to remain protected)
2. do i need to insert the field name within the code?
3. also if i want to use different fonts, do i write the name of the font as
it appears within word (font selection) or spelling from fonts folder?
 
The document has to be unprotected to change the font and then reproteced.
The code is intended to do both. You will have to put your password where I
have used "test"

Sub FormatResult()
Dim oFF As FormField
ActiveDocument.Unprotect Password:="test"
Set oFF = Selection.FormFields(1)
Select Case oFF.Result
Case "SELECTED"
oFF.Range.Font.Name = "Arial Bold"
Case "Not Selected"
oFF.Range.Font.Name = "Arial"
Case Else
'Do Nothing
End Select
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

You do not need to put the field name in the code because the code works on
any selected dropdown field.

I used the font names from the font dropdown menu.
 
Thanks for all your help

Greg Maxey said:
The document has to be unprotected to change the font and then reproteced.
The code is intended to do both. You will have to put your password where I
have used "test"

Sub FormatResult()
Dim oFF As FormField
ActiveDocument.Unprotect Password:="test"
Set oFF = Selection.FormFields(1)
Select Case oFF.Result
Case "SELECTED"
oFF.Range.Font.Name = "Arial Bold"
Case "Not Selected"
oFF.Range.Font.Name = "Arial"
Case Else
'Do Nothing
End Select
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

You do not need to put the field name in the code because the code works on
any selected dropdown field.

I used the font names from the font dropdown menu.



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
 
Reapply the password with the macro:

Sub FormatResult()
Dim oFF As FormField
ActiveDocument.Unprotect Password:="test"
Set oFF = Selection.FormFields(1)
Select Case oFF.Result
Case "SELECTED"
oFF.Range.Font.Name = "Arial Bold"
Case "Not Selected"
oFF.Range.Font.Name = "Arial"
Case Else
'Do Nothing
End Select
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:="test"
End Sub
 
Back
Top