0

Will a kind soul help to provide a vba solution to press a button (e.g 3) in Microsoft calculator without using sendkeys?

Please pardon my bad work as i am new, I believe i am stuck because the windows are not unique.

If anyone has a solution to this problem, please feel free to post your answers. I welcome all alternatives other than sendkeys. Thank you very much.

Below is my failed code.


Private Const BM_CLICK = &HF5
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 Long, lParam As Any) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Sub Calculator()

Dim Program As String, TaskID As Double

Program = "calc.exe"
On Error Resume Next
AppActivate "Calculator"

If Err <> 0 Then
Err = 0
TaskID = Shell(Program, 1)

Do
DoEvents
hwindow2 = FindWindow(vbNullString, "Calculator")

Loop Until hwindow2 > 0

main_view = FindWindowEx(hwindow2, 0&, "Calcframe", vbNullString)
sub_view = FindWindowEx(main_view, 0&, "#32770", vbNullString)
sub_window = FindWindowEx(sub_view, 0&, "Button", vbNullString)

'Call SendMessage(sub_window, BM_CLICK, 0, ByVal0&)

If Err <> 0 Then MsgBox "Can't start " & Program

End If

End Sub
  • Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – Tim Jun 06 '20 at 16:54
  • Dear Tim, sorry for not declaring my purpose clearly. I wanted a solution because i am not sure if my code is the correct way to the problem, therefore i left it as a open ended question so that people can propose different ways of solving it. – API_newnoob Jun 06 '20 at 18:37
  • Awesome! That's now clear. Good luck!! (alas I was just reviewing new user questions, so I lack the subject matter expertise to assist, but I hope that you get some useful feedback) – Tim Jun 06 '20 at 19:58

0 Answers0