PDA

View Full Version : simulating keystrokes



kr651129
08-18-2010, 01:25 PM
Hi all,
I've got a sheet of data and I need to take this data one cell at a time and copy it into a non microsoft program and then simulate keystokes inside that software from my macro. Is there anyway of doing this?

Kenneth Hobs
08-18-2010, 01:43 PM
Sendkeys() would be the first option. That method should always be your last choice though. SendKeys() will not work when Vista's or Win7's UAC is on. You can use alternative programs like AutoIt to send keys irregardless of UAC status. I have written a vb.net EXE file that can do it as well. SendKeys() methods rely on focus and timing. This can be tricky at times.

Some other applications allow access to their object model. That method depends on the application of course.

kr651129
08-18-2010, 01:57 PM
Sendkeys() would be the first option. That method should always be your last choice though. SendKeys() will not work when Vista's or Win7's UAC is on. You can use alternative programs like AutoIt to send keys irregardless of UAC status. I have written a vb.net EXE file that can do it as well. SendKeys() methods rely on focus and timing. This can be tricky at times.

Some other applications allow access to their object model. That method depends on the application of course.

Here is the basic idea.

I am having data provided to me via xls.

That data will then be cleaned up by my macro.

I will then need to preform tasks with this data in another program.
I will need to copy the account number, paste it into my software and hit search. Click a few buttons in said software. Clear the account and move to the next.

kr651129
08-18-2010, 02:32 PM
I've done some more research on this, please tell me if I am on the right path here

I would use AppActivate() to place the focus on the application I need and the I would use SendKey() to emulate the keys to this application?

Kenneth Hobs
08-18-2010, 02:55 PM
There is alot more to it but to some extent , yes. Use SendKeys() as there is no SendKey(). API commands offer some other options.

e.g.
Sub SavePartcorrect()
Dim myPath As String, txtPath As String
Dim rc As Long
Dim wb As Workbook

Set wb = ActiveWorkbook
myPath = ThisWorkbook.Path & "\"
txtPath = myPath & "Test.txt"

rc = Shell("NOTEPAD.EXE " & txtPath, vbNormalFocus)
AppActivate rc
Application.Wait Now + TimeValue("00:00:01")
SendKeys Application.UserName, True
SendKeys "{Enter}", True
End Sub

Here are some API methods to give you an idea of other possibilities. Put this code in a Module. http://vbaexpress.com/forum/showthread.php?t=26601