PDA

View Full Version : Solved: Dropdown in Word VBA



talhamoin
02-13-2011, 09:24 AM
I want to create a macro in vba that should pick a word from document and prompt the user with a drop-down list. This list will have different color names. Depending upon the color selection by the user, font color of that word changes. How can I use drop down in vba?

Tinbendr
02-13-2011, 06:11 PM
See attachment.

Private Sub CommandButton1_Click()
If Len(Selection) > 0 Then
Select Case LCase(Me.ComboBox1)

Case "red"
Selection.Font.ColorIndex = wdRed

Case "blue"
Selection.Font.ColorIndex = wdBlue

Case "green"
Selection.Font.ColorIndex = wdGreen

End Select
End If

End Sub

Private Sub UserForm_Initialize()

With Me.ComboBox1
.AddItem "Red"
.AddItem "Blue"
.AddItem "Green"
End With

End Sub

fumei
02-14-2011, 11:26 AM
Not trying to be critical. Just trying to make suggestions. It does not take much to make your demos more friendly.

1. an error trap for whether the Selection extends beyond a single character. If nothing is selected, the commandbutton clicks does nothing, and seems to do nothingl.

2. an Unload at the end of your commandbutton_click. Otherwise the userforms still sits there and the user is forced to manually exit it.

BTW: If Len(Selection) > 0 Then

is a pointless IF test. Len(Selection) is never 0. A Collapsed Selection range = 1; as does Range itself. Therefore the rest of the insructions will always be executed.

talhamoin
02-14-2011, 11:43 AM
Okay. So, I have to create a user form at run time.

Thanks.

Tinbendr
02-14-2011, 12:51 PM
Not trying to be critical. Just trying to make suggestions. I know. It's just the teacher in you.

It does not take much to make your demos more friendly.A character flaw on my part for sure. Vague questioins get vague answers.



BTW: If Len(Selection) > 0 Then is a pointless IF test. Len(Selection) is never 0. I did test this when one letter was selected, but not when nothing was selected. Strange how nothing when is selected the length is 1 and when one character is selected the length is also 1. VBA does not always play fair.

fumei
02-14-2011, 01:50 PM
"VBA does not always play fair."

LOL! It seems that way, but ultimately it DOES make sense....sort of.

It relates to the document model (as opposed to the object model) Microsoft built. In it, it is NOT possible to have an "empty" or blank document. It is not possible to have a Len(gth) of 0.

Try getting the Len of a header (or footer) that you have never done anything at all with. Use a header that does not even appear - say the Even header for a new document that does NOT have Even headers showing.

Sub Really()
MsgBox Len(ActiveDocument.Sections(1).Headers(wdHeaderFooterEvenPages) _
.Range.Text)
End Sub
What comes back is.....1.

Even though nothing has ever been done.

Frosty
02-17-2011, 11:29 AM
Actually, the fun part is that if you touch that header with code, it will be perceived as touched (and if you are showing paragraph marks, a paragraph mark will appear), but if you double-click into and out of a header... that paragraph mark will disappear.

In a blank new document, with your paragraph marks showing, try the following in the immediate pane:
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete

Now manually click into the header, and exit... the paragraph mark disappears.

It's Microsoft's way of answering the tree falling in a forest question, I think.

You could achieve your Len(Selection) test by using len(Selection.Range)

This is one of the many reasons to avoid the Selection object, I think. The Range object works more as you'd expect.

That said, as Gerry said... you'll never have a Len of 0 for any of the various word stories... because (regardless of whether you're showing paragraph marks or not, or MS is showing them or not), all of those document stories exist: even/odd/primary headers.

But you can have a len 0 of an insertion point... if you ask in the right way. Either Selection.Range or Range.Start = Range.End, etc...

fumei
02-17-2011, 12:32 PM
I like this guy (? gal?).

Frosty is indeed correct. For a collapsed Selection...

Len(Selection) = 1
Len(Selection.Range) = 0

And again, this comes from the document model. The Len of the Selection always includes something. But a collapsed Selection has a range Start = End...and therefore End - Start = 0.

Frosty
02-17-2011, 01:20 PM
Thanks. Is this the last month, or is has your signature always said you're a month away from retirement?

I actually agree with Tinbendr. VBA doesn't play fair, and I think this kind of thing actually doesn't make sense unless you understand that the selection object is not simply a range object represented by the current selection.

It's a totally separate object with different programming behind it. And while it shares a lot with the range object, there is a *lot* of extra klugey programming on Microsoft's part, because, well, the selection object is just about as important an object as there is... because while most end-users will never deal with the actual range object, everyone will deal with the selection object.

Best practice for actual programming (not recording macros) would be (I believe) to stay away from the selection object entirely (there may be *occasional* times when you can't, but much more rare than one would think).

Most of the time, if you're doing something which requires use of the selection object... it's probably something that could be solved using training for the end-user.

To bring it full circle... the original poster wanted to change the color of a word. Why not just right-click the word, and click the Text Color button? There are a lot of options there.

Unless it's a classroom exercise (in which case, shouldn't we try to help point the user towards custom modal dialog boxes and how to customize context menus in the help files), that would seem the be the best answer to me.

It can be a fun exercise to re-invent the wheel... but MS has already done a lot of this stuff for us. Especially in Word 2007/Word 2010 (although Font color existed on the right-click menu in earlier versions, just buried in the font dialog).

Just a thought ;)

fumei
02-18-2011, 12:32 PM
I think it has been repeatedly expressed here (and many other places) that, in general, when coding it is far better to use Range, rather than Selection.

For a number of reasons. Selection is primarily what is on screen, the GUI. Therefore ALL actions using Selection take up resources (the GUI) and time. Plus there are methods and properties available with Range that are not available with Selection. Not many, but some. There are a few (just a few) situations where Selection works, and Range does not.

Gotta change my signature...<100 hours to go.

talhamoin
03-07-2011, 09:47 AM
Thanks guys. It was a fruitful discussion.