PDA

View Full Version : Solved: Formfield Shading



Shred Dude
05-02-2008, 01:04 PM
I'm trying to shade a formfield via code, and having trouble.

With my macro recorder on ,I can't even do what i can do without it on. If I float my cursor over a formfield, a get a small window that allows me to set various properties of that formfield. including shading. I'd like to do this with code.

If I select the formfield and do:
selection.Shading.BackgroundPatternColor =wdColorRed
nothing happens. Unless the formfield is within a table cell, then the whole cell gets shaded, but not the formfield's gray box.

I haven't been able to find that little window in the WdWordDialog collection to attempt that route. I was hoping to be able to do wiht code what I can do manually.

Can anyone help?

My goal is to go through a document and shade each formfieild where formfield.result = "".

fumei
05-02-2008, 04:50 PM
When posting, please always state what is version you are asking about. Perhaps you are using 2007, and it allows shading of formfields.

If so, check VBA Help and the Object Browser.

In earlier versions you can not change the shading. It is either ON, or it is OFF.

However, that being said, you CAN do something with the range.

If ActiveDocument.FormFields(1).Result = "" Then
ActiveDocument.FormFields(1).Range.HighlightColorIndex = wdDarkRed
Else
ActiveDocument.FormFields(1).Range.HighlightColorIndex = wdNoHighlight
End If

I put this as the OnExit code for a different formfield - FormFields(2). So it checks back and alters the range of FormField(1), depending on whether (1) is "", or not.

fumei
05-02-2008, 04:56 PM
Further, maybe 2007 is different, but shading for formfields is global - i.e. the shading setting affects ALL formfields.

ActiveDocument.FormFields.Shaded

There is no mechanism for affecting the shading of any ONE formfield.

Shaded - if you look in Object Browser - is a member of FormFields. Notice the plural.

Not FormField.

Shred Dude
05-02-2008, 07:48 PM
Yeah, I had noticed that Shaded was a property of the Collection, not for individual formfields. Thus the beginnning of my confusion. Where was the property for me to update?

Maybe "Shading" wasn't the right term, more accurately it's "highlight" as you point out with your code example. But in Word 2007, I can float my cursor over the formfeild, and a window will pop up offering me all (or at least many) of the formatting icons, including the one that looks like a little paint can, the highlighting icon. You click that and you get choices of colors that will highlight the formfield. I was thinking that if I could do it with mouse clicks, maybe I could find that dialog, set a property and do a .execute. I didn't find it though.

Thanks for making me read the deatils around that little icon. There is an option to "Stop Highlighting"...I should've caught that.

Thanks for the code idea on the OnExit field, that could be very useful.