Macro Help

  • Thread starter Travis Montgomery
  • Start date
T

Travis Montgomery

Hello all,

I'm trying to write a macro that will run upon exiting a dropdown box in
a protected word form. What I'd like to do is, when a user chooses an
option in the dropdown box other than the fist choice, the text in the
box is changed to bold. So far I have this code:

ActiveDocument.Unprotect
Selection.Font.Bold = True
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset

This gets me half way, if a user clicks on the box it changes to bold.
What I think I need now is an If then statement that checks the selected
text and if it does not equal "no change" (the default entry in the
dropdown) it bolds the text.

Can this be done, can anyone fill in the code that I need?

Thanks,

Travis
 
J

Jay Freedman

Hi Travis,

Try this as the exit macro for the dropdown:

Sub ExitDD1()
ActiveDocument.Unprotect
If Selection.FormFields(1).DropDown.Value = 1 Then
Selection.Font.Bold = False
Else
Selection.Font.Bold = True
End If
ActiveDocument.Protect wdAllowOnlyFormFields, True
End Sub


This code should be used *only* for a dropdown; running it on exit from any
other kind of form field will cause an error.

The .Value property is the number of the selected item in the dropdown list.
I used both the If and the Else so that if the user goes back and chooses
the first item again, the bolding is turned off.
 
T

Travis Montgomery

Jay,

Thanks for the quick response. This doesn't seem to work though I'm
guessing I missed a step as I I'm new at this. Here's what I did.

1) Tools/macro/create/pasted the code you sent.
2) Double clicked on each drop down field and in the exit macro box
selected the macro
3) Saved the document/protected the document
4) Went to dropdown, selected 2nd option, left dropdown.

The text didn't change to bold. Any more ideas?

Thanks,

Travis
 
J

Jay Freedman

Travis,

It sounds like it should have worked, but there are always a few nasty
little details.

Open the macro editor (Alt+F11) and locate the macro. It should be in
a module named NewMacros in the Normal project (that's where the
Create button puts it by default).

Put the cursor on the Unprotect line and press F9 to insert a
breakpoint. Now go back to the document and tab out of the dropdown.
The macro editor should pop up with that line highlighted. Press F8
and watch the highlighting. Which lines does it go through?

For more information, right-click the word Value and select Add Watch,
and click OK in the watch dialog. As you step through the macro with
F8, what value shows in the watch window below?

Assuming the line with Bold = True executes, what shows in the field?

And yes, I did test the macro before I posted it, and it works as
advertised for me. :)
 
T

Travis Montgomery

And yes, I did test the macro before I posted it, and it works as
advertised for me. :)

Of this I had no doubt, this is why I was sure I was doing something
wrong. I still don't know what it was but it seems to be working now.
I deleted the macro I had created. Opened a fresh copy of the document,
tried again, seems to be working fine.

One more question: Will the macro follow the document?

Thanks,

Travis
 
J

Jay Freedman

I'm glad it worked eventually!
Will the macro follow the document?

Not if it's in your Normal.dot template (the Normal project as shown in the
macro editor).

You can select the project for the document and click Insert > Module. Then
cut the macro code from the Normal project and paste it into the new module,
and save the document. Then the macro will follow the document to another
PC. The downside of this is that if the other PC's macro security level (in
Tools > Macro > Security) is set to Medium, the user will get a warning
dialog every time they open the document, and have to click Enable to let
the macro work. If the level is set to High, there won't be any dialog and
the macro will be disabled -- it just won't execute at all. This means when
you send the form to people to fill out, you need to tell them to set the
level to Medium and click Enable.
 

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