+ Reply to Thread
Results 1 to 7 of 7

Windows API and SendKeys

  1. #1
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Windows API and SendKeys

    Hi,

    I've been trying to create a macro that used Windows API find an active program's window, activate it, and send the combinations Alt + M to it. I keep failing and thought I might seek some help from anyone with experience using Windows API. The Class name is "Oberon App" and the Window name is "WinBUGS14".

    Thanks.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Windows API and SendKeys

    Are you using a lot of DoEvents in the code to wait for the correct window to be found and after the SendKeys statement so the correct app processes the keys?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Windows API and SendKeys

    Hi Marvin,

    No, there are no DoEvents at all. I can find the handle using the following code:

    [Modified code below - added new response in post #4]

    Please Login or Register  to view this content.
    I'm having trouble then using the handle to activate the window and sending the key strokes to it.

    abousetta
    Last edited by abousetta; 05-23-2013 at 09:41 PM. Reason: Modified code

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Windows API and SendKeys

    Hi Marvin,

    Your post made me realize the problem. The window is losing focus too quickly so that the Alt + M doesn't have enough time to get through. I repeated the SendKeys command five times and it seems to work. Do you know of any workaround to this? Can this be done using API's instead?

    abousetta

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Windows API and SendKeys

    That is the point. You need to insert "DoEvents" enough to wait for the other App to be sure to open before you do the SendKeys. Then more DoEvents for the other App to process them out of the keyboard buffer before your come back to Excel and continue your code....

    I've spent many hours trying to determine how to insert DoEvents. It seems the DoEvent command in Excel allows the OS to work and/or complete other processes it is working on. Assume you have a lot of background apps working. I'm not sure how to determine if the correct app is processing and active before you shove keystrokes into the keyboard buffer....

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Windows API and SendKeys

    I use DoEvents right before the Next in almost all my loops to allow me to escape the loop if needed. I'm not familiar with SendKeys and so not sure how Excel will know if the other program is in a ready state. Like for Internet Explorer we use a loop and doevents waiting for the page to load, but not sure this can be done with this program because there is no direct connection with Excel.

  7. #7
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Windows API and SendKeys

    Alright... finally figured it out using the Windows API keybd_event instead of SendKeys.

    Steps:

    1) get window handle for window I want to send keystrokes to
    2) Lock the window in the foreground so that it received the keystrokes
    3) send the keystrokes

    Sounds simple enough.

    Here is code I ended up putting together:

    Please Login or Register  to view this content.
    Hope this helps someone else.

    abousetta

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1