Results 1 to 10 of 10

Thread: Word VBA Document form....[RESOLVED]

  1. #1

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Word VBA Document form....[RESOLVED]

    I need to have a dropdown in a word form dropdown automatically...any clue??

    The closest I cant get is have it get focus.....
    Last edited by Static; May 5th, 2004 at 09:25 AM.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  2. #2
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    How do you mean automatically, when the document is opened or what?

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This works in VB. I'm sure it should work in Word.
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    2. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    3.  
    4. Const CB_SHOWDROPDOWN = &H14F
    5.  
    6. Private Sub Command1_Click()
    7.     SendMessage cboDropMe.hwnd, CB_SHOWDROPDOWN, ByVal 1, ByVal 0&
    8. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Uh, edit that.
    Word equilivalent.
    VB Code:
    1. Private Sub CommandButton1_Click()
    2.     cboDropMe.DropDown
    3. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    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!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    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.
    VB Code:
    1. Private Sub Document_Open()
    2.     Call LoadMe
    3. End Sub
    4.  
    5. Sub LoadMe()
    6.  
    7.     Dim oFF As FormField
    8.     Dim i As Integer
    9.    
    10.     If ActiveDocument.FormFields.Count = 0 Then
    11.         ActiveDocument.FormFields.Add ActiveDocument.Range(Start:=0, End:=0), wdFieldFormDropDown
    12.     End If
    13.     Set oFF = ActiveDocument.FormFields(1)
    14.     oFF.DropDown.ListEntries.Clear
    15.     For i = 1 To 10
    16.         oFF.DropDown.ListEntries.Add "Test " & i, i
    17.     Next
    18.     ActiveDocument.Protect wdAllowOnlyFormFields
    19.     SendKeys "%{DOWN}", True
    20.     'oFF.DropDown.Value = 8 'Select the 8th element in the dropdown list.
    21.  
    22. End Sub
    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    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
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    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:
    1. Public Sub Check1()
    2.  
    3.     Dim oFF As FormField
    4.     Dim i As Integer
    5.    
    6.     If ActiveDocument.FormFields.Item("Check1").CheckBox.Value = True Then
    7.         Set oFF = ActiveDocument.FormFields.Item("Dropdown1")
    8.         oFF.DropDown.ListEntries.Clear
    9.         For i = 1 To 10
    10.             oFF.DropDown.ListEntries.Add "Test " & i, i
    11.         Next
    12.         If ActiveDocument.ProtectionType <> wdAllowOnlyFormFields Then
    13.             ActiveDocument.Protect wdAllowOnlyFormFields
    14.         End If
    15.         SendKeys "+{TAB}", True
    16.         SendKeys "%{DOWN}", True
    17.     End If
    18.  
    19. End Sub
    20.  
    21. Public Sub Check2()
    22.  
    23.     Dim oFF As FormField
    24.     Dim i As Integer
    25.    
    26.     If ActiveDocument.FormFields.Item("Check2").CheckBox.Value = True Then
    27.         Set oFF = ActiveDocument.FormFields.Item("Dropdown2")
    28.         oFF.DropDown.ListEntries.Clear
    29.         For i = 2 To 10 Step 2
    30.             oFF.DropDown.ListEntries.Add "Test " & i, i / 2
    31.         Next
    32.         If ActiveDocument.ProtectionType <> wdAllowOnlyFormFields Then
    33.             ActiveDocument.Protect wdAllowOnlyFormFields
    34.         End If
    35.         SendKeys "+{TAB}", True
    36.         SendKeys "%{DOWN}", True
    37.     End If
    38.    
    39. End Sub
    40.  
    41. Public Sub Check3()
    42.  
    43.     Dim oFF As FormField
    44.     Dim i As Integer
    45.    
    46.     If ActiveDocument.FormFields.Item("Check3").CheckBox.Value = True Then
    47.         Set oFF = ActiveDocument.FormFields.Item("Dropdown3")
    48.         oFF.DropDown.ListEntries.Clear
    49.         For i = 3 To 10 Step 3
    50.             oFF.DropDown.ListEntries.Add "Test " & i, i / 3
    51.         Next
    52.         If ActiveDocument.ProtectionType <> wdAllowOnlyFormFields Then
    53.             ActiveDocument.Protect wdAllowOnlyFormFields
    54.         End If
    55.         SendKeys "+{TAB}", True
    56.         SendKeys "%{DOWN}", True
    57.     End If
    58.    
    59. End Sub
    Attached Files Attached Files
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    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!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    No problem. Glad to help.
    I learned more too.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width