Results 1 to 7 of 7

Thread: [RESOLVED] Event doesn't fire when filling external combobox with sendmessage

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    24

    Resolved [RESOLVED] Event doesn't fire when filling external combobox with sendmessage

    REI need to use the "Save As" dialog box to save a file on the Desktop. (Windows XP) To do that I made this code to change value of the combobox on the top combobox to "desktop". When I run it, the text changes to desktop, but the change event doesn't fire, which means it will still save in the default folder. If I click on the combobox and away maually after running this code that fires the event and the destination folder actually changes, but I can't do this with code. I don't want to use sendkeys and simulated mouse clicks as they are not so reliable. If I could simulate typing "Desktop" in the combobox that could also work, but I didn't manage to that either.

    Code:
    Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Const CB_FINDSTRINGEXACT = &H158
    Const CB_SETCURSEL = &H14E
    
    Sub test()
    Dim hwnd As Long, cbox As Long, itemid As Long
    hwnd = FindWindow("#32770", "Save as")
    cbox = FindWindowEx(hwnd, 0&, "combobox", vbNullString)
    itemid = SendMessage(cbox, CB_FINDSTRINGEXACT, 0, "Desktop")
    SendMessage cbox, CB_SETCURSEL, ByVal itemid, ByVal itemid
    End Sub
    Please help it took me really long time to get this far.

    P.S: There's no easier way. This is part of an exporting process of non-microsoft application which uses the "Save As" dialog box.
    Last edited by tahi.laci; Jun 7th, 2013 at 01:38 PM. Reason: Max187Boucher's advice

  2. #2
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Event doesn't fire when filling external combobox with sendmessage, only text cha

    First of all you should never name your combobox "combobox".

    Second your FindWindow is wrong change the &O0 to 0&

    Third your SendMessage is also wrong having combobox as name is not good, and even if it did work you need it's HWND so it would be combobox.hwnd

    I did not test your code, I am on my phone

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    24

    Re: Event doesn't fire when filling external combobox with sendmessage, only text cha

    I changed the variable name from "combobox", and &O0 to 0&. The result is still the same. If I put .hwnd after the variable name in sendmessage I get "Invalid Qualifier" error.

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Event doesn't fire when filling external combobox with sendmessage, only text cha

    Even when the code is corrected, the API doesn't seem to cause any ComboBox events. I suggest you move the code in the _Click event to a Subroutine (eg Sub ComBoxClicked) and call that subroutine immediately after you've sent the CB_SETCURSEL.
    e.g

    Code:
    Sub test()
    Dim hwnd As Long, lngCombobox As Long, itemid As Long
    hwnd = FindWindow(vbNullString, "Save as")
    lngCombobox = FindWindowEx(hwnd, 0&, "ThunderComboBox", vbNullString)
    itemid = SendMessage(lngCombobox, CB_FINDSTRINGEXACT, 0, "Desktop")
    SendMessage lngCombobox, CB_SETCURSEL, ByVal itemid, ByVal 0&
    cmbClicked
    End Sub
    
    Private Sub cmbClicked()
    Debug.Print cmb.Text
    End Sub
    
    Private Sub cmb_Click()
    cmbClicked
    End Sub
    
    Private Sub Command_Click()
    test
    End Sub
    
    Private Sub Form_Load()
    cmb.AddItem "Fred"
    cmb.AddItem "Bert"
    cmb.AddItem "Desktop"
    End Sub
    If you need to, you could do the same with the _Change event code, if you don't want to use the _Click event. Whichever method you use the idea is to simulate the event after using the API to do what you want.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    24

    Re: Event doesn't fire when filling external combobox with sendmessage, only text cha

    On the cmb.text line I get the "Variable not defined." error. If I leave that part out the code does the same as it used to.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    24

    Re: Event doesn't fire when filling external combobox with sendmessage, only text cha

    I used sendmessage to double-click on the combobox, which fires the Click event. I'm sure this is not the "official" solution, but it works and I believe it's reliable.

    Code:
    Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Const CB_FINDSTRINGEXACT = &H158
    Const CB_SETCURSEL = &H14E
    Const WM_LBUTTON_DOWN = &H201
    Const WM_LBUTTON_UP = &H202
    
    
    Sub test()
    Dim hwnd As Long, cmb As Long, itemid As Long
    hwnd = FindWindow(vbNullString, "Save As")
    cmb = FindWindowEx(hwnd, 0&, "ComboBox", vbNullString)
    
    Call SendMessage(cmb, WM_LBUTTON_DOWN, ByVal 0&, 0&)
    Call SendMessage(cmb, WM_LBUTTON_UP, ByVal 0&, 0&)
    
    itemid = SendMessage(cmb, CB_FINDSTRINGEXACT, 0, "Desktop")
    SendMessage cmb, CB_SETCURSEL, ByVal itemid, ByVal 0&
    
    Call SendMessage(cmb, WM_LBUTTON_DOWN, ByVal 0&, 0&)
    Call SendMessage(cmb, WM_LBUTTON_UP, ByVal 0&, 0&)
    End Sub
    I had to make a little change. In the program I use wrong values were selected. when I put the two "mouse -clicks" next to each other. Apparently when you click on the combobox in that program the list of values change. I believe before you click on the combobox all the possible values are included but when you click the currently selected value will be removed.
    Last edited by tahi.laci; Jun 7th, 2013 at 01:27 PM.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Event doesn't fire when filling external combobox with sendmessage, only text cha

    i did an example to do this, with an IE saveAs dialog, in office development forum, but i am sure it is no better than your solution, i remember i had to put a delay, for similar reasons
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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