Use AppActivate to change the active window

后端 未结 2 1982
感动是毒
感动是毒 2021-01-20 10:19

I\'m am trying to send some keystrokes to a program. I have some example code below which works fine up until the final {Alt} command. I believe this is due to

相关标签:
2条回答
  • 2021-01-20 11:00
    1. You're using Shell on your last line but you haven't defined it. Change it to objShell instead.
    2. To send the Alt key, use "%". For example:

      objShell.SendKeys "%"
      

      You would typically use this in a key combination. For example, to open the File menu in most programs, you would use Alt+F, or:

      objShell.SendKeys "%f"
      
    3. If your window title changes, or you need to activate a new window for whatever reason, just call AppActivate again:

      objShell.AppActivate "My new window title"
      
    0 讨论(0)
  • 2021-01-20 11:00

    Each windows process has an identifier. If you store the process id for each notepad window, you can avoid worrying about finding the right window after its title changes.

    Here is an example of opening two notepad files, activating by process id and sending keys.

    Set objShell = WScript.CreateObject("WScript.Shell")
    
    Function SendKeysTo (process, keys, wait)
        objShell.AppActivate(process.ProcessID)
        objShell.SendKeys keys
        WScript.Sleep wait
    End Function
    
    Set notepadA= objShell.Exec("notepad")
    Set notepadB= objShell.Exec("notepad")
    WScript.Sleep 500
    
    
    SendKeysTo notepadA, "Hello I am Notepad A", 1000
    SendKeysTo notepadB, "Hello I am Notepad B", 1000
    

    Hopefully you can take a similar approach to solve your problem.

    0 讨论(0)
提交回复
热议问题