0

I am writing a VBA to write text in a third party program. There are 2 window class name with the same name "Edit" under "subwin" - "wxWindowNR".

I am able to send message to the first "Edit" but not the second one.

How am i able to send text to the second "Edit?

Sub writeTextOnWin()
Dim i As Long
Dim wClass As Object

hWnd = FindWindow(vbNullString, "mkvmerge GUI")

start_doc = ShellExecute(hWnd, "open", "C:\Program Files\MKVToolNix\mmg.exe", "", 0, SW_NORMAL)

If start_doc = 2 Then Exit Sub
If start_doc = 3 Then Exit Sub

Do
DoEvents
hwindow2 = FindWindow(vbNullString, "mkvmerge GUI v7.7.0 ('Six Voices') 64bit")
Loop Until hwindow2 > 0


mainWin = FindWindowEx(hwindow2, 0&, "wxWindowNR", vbNullString)
subWin = FindWindowEx(subWin, 0&, "wxWindowNR", vbNullString)

editWin = FindWindowEx(subWin, 0&, "Edit", vbNullString)
editWin2 = FindWindowEx(subWin, 0&, "Edit", vbNullString)

Call SendMessageByString(editWin, WM_SETTEXT, 0, "hello")
Call SendMessageByString(editWin2, WM_SETTEXT, 1, "hello again")

end sub
pexpex223
  • 371
  • 4
  • 10
  • 25

1 Answers1

0

FindWindowEx is the wrong tool for the job here. Both of the windows that you look for have the same class name, and since the window text is not predictable, FindWindowEx is not appropriate.

Instead you need to iterate over the children of subWin looking for the two edit windows. There are two obvious ways to do that:

  1. Call EnumChildWindows to enumerate all of the children of subWin. You will need to provide a callback function for this. This is perfectly possible in VBA but requires more complexity than your current code.
  2. Use GetWindow to walk over the children of subWin. Pass GW_CHILD on the first call to get the first child. Then, call again passing the latest child and GW_HWNDNEXT. Keep calling GetWindow until you've found your windows.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490