-1

Simplified and working code below, but only works once then not again until the window is restarted. Is there some sort of finish set text missing or some other limitation? Can't find any results on google, Thanks

import win32api
import win32gui
import win32con

handle = windowName #Script is working with actual window name
mainWindowHWND = win32gui.FindWindow(None, handle)

win32api.SendMessage(mainWindowHWND, win32con.WM_SETTEXT, 0, "test")

  • My guess is that, after changing the window's name, you try to find it again by the original name, which fails. – Adrian McCarthy Jan 08 '23 at 16:33
  • You nailed it, I used spy++ to confirm. The text was going where it was supposed to go inside the window so I never considered the window caption was also being changed. It's definately the correct window to send the text to, just need to not also change the window name, working on it now unless someone beats me to the answer, thanks @AdrianMcCarthy – user3489221 Jan 08 '23 at 19:09
  • I gave up and did a workaround, appreciate anyone with the answer =) – user3489221 Jan 08 '23 at 21:42

1 Answers1

0

You need to find the exact control handle in order to send the text. Now, you are changing the window title of that program. So Assume that you are setting a notepad window's title to 'test'. Then it become a window with title 'test'. So you can't get the window handle again with the old text. You need to enumerate the all the child windows of that specific window and check the type of control you are interested in. Then set the text of that control You can use EnumChildWindows api function for that. https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumchildwindows

Vinod KC
  • 29
  • 2
  • 9