• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 821
  • Last Modified:

Automating a button click in VB, not working though I have the handle to the button/control?

I hope this is as easy as it seems, I'm missing something though as I'm not well versed in VB.

Once I've got the  Print/Save dialog from of the browswer,  I'm sucessfully getting a handle to the print dialog window and also the handle for the "save as" button however getting the button to click is not working.

Additionally, I'd like to be able to send a file name directly to the textbox within this dialog box instead of using keystrokes.

Simple right?

I've enclosed some scaled down code to make for easy scanning, basically I'm stuck in the process of getting SendMessage() to  
DialogHwnd = 0
        Do Until DialogHwnd <> 0
            DialogHwnd = FindWindow(vbNullString, "Save As")
        Loop
        
                
        
        
    Dim ButtonHwnd As Long
    

    'get the handle of the first button
       
    ButtonHwnd = FindWindowEx(DialogHwnd, 0, "Button", "&Save")
               
        Call SendMessage(ButtonHandle, ByVal BM_CLICK, 0, 0)

Open in new window

0
srj19
Asked:
srj19
  • 6
  • 3
  • 2
  • +4
1 Solution
 
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
Unless I am missing somthing , you are getting the handle ito the ButtonHwnd variable yet you are passing ButtonHandle to the SendMessage function.

Where is ButtonHandle getting set to the value of ButtonHwnd?
0
 
Bryan ButlerCommented:
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For the button, try a sequence like this:

    Call SendMessage(ButtonHandle, WM_LBUTTONDOWN, 0, 0)
    Call SendMessage(ButtonHandle, BM_SETSTATE, 1, 0)
    Call SendMessage(ButtonHandle, BM_CLICK, 0, 0)
    Call SendMessage(ButtonHandle, WM_LBUTTONUP, 0, 0)

Are you sure you have a valid handle though?

    ButtonHwnd = FindWindowEx(DialogHwnd, 0, "Button", "&Save")
    If ButtonHwnd <> 0 Then
        ' ... click the button ...
    Else
        MsgBox "Button not found"
    End If
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
srj19Author Commented:

Idle_Mind, I inserted the code you gave (buttondown, setstate, click and button up) but I still am not clicking the button. I'm pretty sure the handle to the button is good, I put a few msgboxs into the code and after identifiying the window I have a handle of 268688 and after identifing the button I have a control of 203416. Assuming the handle is good, what is likely the cause of the failure to click? This code would work on any windows based button correct? This button is part of the CutePdf printer/convter dialog. It functions just like printing but converts to a PDF.


xDJR1875, I'm using SendMessage since it's the only example I've seen online to communicate with the button control, is there a better way to do this?

DevelopedTester, I looked at the link you sent. I initially thought I'd written it since he described my situation so closely, but in the end it looks like he didn't resolve the issue in that forum.
0
 
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
srj19... I simply stated that it looked like you were assigning the button handle to one variable (ButtonHwnd ) and then using a different handle (ButtonHandle ) to send the message.

Where does ButtonHandle get the windowhandle id from? If ButtonHwnd has the value, use it in the SendMessage call.
0
 
srj19Author Commented:
I just noticed that in the example I posted the two handle variables were not the same but since then I've tried Idle_Minds' additional code plus some from another source and I'm still not getting a click.

To answer you other question, ButtonHwnd gets value from the line
 ButtonHwnd = FindWindowEx(DialogHwnd, 0, "Button", "&Save")

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you can't get it to work with SendMessage() then you can pass the handle to GetWindowRect() which would give you the physical screen coords of the button.  Then you could physically move and click the mouse over that area using the mouse_event() API.  This obviously has the limitation that the dialog MUST be in the foreground for this to work.
0
 
srj19Author Commented:
I might have to go the direction of moving the mouse and clicking the button via programming. ACan you point me toward any code examples of how that would work?

On the other hand, is there any code snipets I could be putting into the flow that could help undertand what is not being passed to SendMessage() or possibly what isn't being returned.

Scott
0
 
srj19Author Commented:
Anyone?

Assuming I have a valid handle for the "Save As" button, what are the areas to delve into to determine why the code below isn't producing a click?

Call SendMessage(ButtonHandle, WM_LBUTTONDOWN, 0, 0)
Call SendMessage(ButtonHandle, BM_SETSTATE, 1, 0)
Call SendMessage(ButtonHandle, BM_CLICK, 0, 0)
 Call SendMessage(ButtonHandle, WM_LBUTTONUP, 0, 0)

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure...do you have Spy++?  It would help in figuring out if the targeted window is getting the message or determining if a different message is needed instead.
0
 
nffvrxqgrcfqvvcCommented:
You can look at this accepted answere and see if it helps .. http:Q_25718823.html

Which OS and can you show print screen of the this print dialog?
0
 
ArkCommented:
Not sure about WebBrowser dialogs, but standard windows dialogs use WM_COMMAND messages

Option Explicit

Private Enum CtrlID ' ID of controls
' For All dialogs
     ID_OK = &H1  'Open or Save button
     ID_CANCEL = &H2 'Cancel Button
     ID_HELP = &H40E 'Help Button
' For open/save dialogs
     ID_READONLY = &H410 'Read-only check box
     ID_FILETYPELABEL = &H441 'FileType label
     ID_FILELABEL = &H442 'FileName label
     ID_FOLDERLABEL = &H443 'Folder label
     ID_LIST = &H461 'Parent of file list
     ID_FILETYPE = &H470 'FileType combo box
     ID_FOLDER = &H471 'Folder combo box
     ID_FILETEXT = &H480 'FileName text box
     ID_NEWFOLDER = &HFFFFA002 ' NewFolder Button - can not be disabled
     ID_PARENTFOLDER = &HFFFFA001  ' GoUp button  - can not be disabled
' for print dialogs
     ID_ALLPAGES = &H420
     ID_SELECTEDTEXT = &H421
     ID_PAGERANGE = &H422
     ID_COPYES = &H482
     ID_PRINTERCOMBO = &H473
     ID_PRINTTOFILE = &H410
     ID_PROPERTIES = &H401
 ' for font Dialog
     ID_FONTTEXT = &H470
     ID_STYLETEXT = &H471
     ID_SIZETEXT = &H471
End Enum

Private Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long

Private Const WM_COMMAND = &H111
Private Const BN_CLICKED = 0

Private Sub Command1_Click()
   'Assuming you know hDlg - dialog handle
   PostMessage hDlg, WM_COMMAND, MakeDWord(ID_OK, BN_CLICKED), GetDialogItem(hDlg, ID_OK)
End Sub

Private Function GetDialogItem(ByVal hDlg As Long, ByVal ItemID As CtrlID) As Long
   GetDialogItem = GetDlgItem(hDlg, nItem)
End Function

Private Function MakeDWord(ByVal wLo As Integer, ByVal wHi As Integer) As Long
    MakeDWord = (wHi * 65536) + (wLo And &HFFFF&)
End Function

Open in new window

0
 
ArkCommented:
Oops, PostMessage Declaration:
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, Byval lParam As Long) As Long
0
 
duncanb7Commented:
I  always use Postmessage and it work for many year for a button click for auotmation file download in popupmenu,
Sendmessage is not stable

Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_LBUTTONDOWN = &H201
hdlg= FindWindow(vbNullString, "Example download site! - Window Internet Explorer")
retval= 0
Do Until retval <> 0
retval = PostMessage(hdlg, WM_LBUTTONDOWN, 0, vbNullString) 'Click download button
Loop
0
 
srj19Author Commented:
Wasn't able to resolve.
0
 
srj19Author Commented:
no solution
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 6
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now