I need to have a dropdown in a word form dropdown automatically...any clue??
The closest I cant get is have it get focus.....
Printable View
I need to have a dropdown in a word form dropdown automatically...any clue??
The closest I cant get is have it get focus.....
How do you mean automatically, when the document is opened or what?
This works in VB. I'm sure it should work in Word.
VB Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Const CB_SHOWDROPDOWN = &H14F Private Sub Command1_Click() SendMessage cboDropMe.hwnd, CB_SHOWDROPDOWN, ByVal 1, ByVal 0& End Sub
Uh, edit that.
Word equilivalent.
VB Code:
Private Sub CommandButton1_Click() cboDropMe.DropDown End Sub
Thanks for the reply...but...this isnt a VB form in word...its a document form. (FORMS toolbar)
so the darn things cant be refered to like that... its
ThisDocument.Formfields(x).etc
aaauuughhh!
Ok, try this in a new blank document (add code to ThisDocument
class). Save it, close it, and re-open it and it will automatically add
a dropdown with 10 items, already dropped menu.
HTHVB Code:
Private Sub Document_Open() Call LoadMe End Sub Sub LoadMe() Dim oFF As FormField Dim i As Integer If ActiveDocument.FormFields.Count = 0 Then ActiveDocument.FormFields.Add ActiveDocument.Range(Start:=0, End:=0), wdFieldFormDropDown End If Set oFF = ActiveDocument.FormFields(1) oFF.DropDown.ListEntries.Clear For i = 1 To 10 oFF.DropDown.ListEntries.Add "Test " & i, i Next ActiveDocument.Protect wdAllowOnlyFormFields SendKeys "%{DOWN}", True 'oFF.DropDown.Value = 8 'Select the 8th element in the dropdown list. End Sub
Your close rob,
I tried the sendkeys as well... but, when there are multiple dropdowns...it just jumps to the next dropdown and sets focus. it doesnt drop down!!!!
I have 6 checkboxes...each with a dropdown next to it.
when I click the check box I wanted the dropdown to open...
Best I can get is just set focus to the dropdown.
Oh well....close enough for now.
Thanks for your help
Ok, here is the solution.
I clear and reload each dropdown when the corresponding
checkbox is checked. This seems to keep focus on the
corresponding line. Then a sendkeys of shift + tab to set the
focus on the correct dropdown and another sendkeys to drop it
down. Micky mouse but it works. I attached my test doc for you to
look at.
VB Code:
Public Sub Check1() Dim oFF As FormField Dim i As Integer If ActiveDocument.FormFields.Item("Check1").CheckBox.Value = True Then Set oFF = ActiveDocument.FormFields.Item("Dropdown1") oFF.DropDown.ListEntries.Clear For i = 1 To 10 oFF.DropDown.ListEntries.Add "Test " & i, i Next If ActiveDocument.ProtectionType <> wdAllowOnlyFormFields Then ActiveDocument.Protect wdAllowOnlyFormFields End If SendKeys "+{TAB}", True SendKeys "%{DOWN}", True End If End Sub Public Sub Check2() Dim oFF As FormField Dim i As Integer If ActiveDocument.FormFields.Item("Check2").CheckBox.Value = True Then Set oFF = ActiveDocument.FormFields.Item("Dropdown2") oFF.DropDown.ListEntries.Clear For i = 2 To 10 Step 2 oFF.DropDown.ListEntries.Add "Test " & i, i / 2 Next If ActiveDocument.ProtectionType <> wdAllowOnlyFormFields Then ActiveDocument.Protect wdAllowOnlyFormFields End If SendKeys "+{TAB}", True SendKeys "%{DOWN}", True End If End Sub Public Sub Check3() Dim oFF As FormField Dim i As Integer If ActiveDocument.FormFields.Item("Check3").CheckBox.Value = True Then Set oFF = ActiveDocument.FormFields.Item("Dropdown3") oFF.DropDown.ListEntries.Clear For i = 3 To 10 Step 3 oFF.DropDown.ListEntries.Add "Test " & i, i / 3 Next If ActiveDocument.ProtectionType <> wdAllowOnlyFormFields Then ActiveDocument.Protect wdAllowOnlyFormFields End If SendKeys "+{TAB}", True SendKeys "%{DOWN}", True End If End Sub
thanks Rob, but your sendkey code from before triggered a new Idea...(Which you have just used)
in the on enter macro for the checkbox...
Sendkeys "{TAB}%{DOWN}"
worked perfectly!!
thanks for your help!
No problem. Glad to help.
I learned more too.