We help IT Professionals succeed at work.

Can I use the SendKeys command in Excel VBA to interact with other applications other that Microsoft Office?

macrohappy
macrohappy asked
on
I am in the middle of a macro that performs several different functions one of them runs a .bat file that then opens a program that requires me to sign into. Is there a way to make that program an Object so I can use the macro and Sendkeys (or something similar) commands to sign in automatically?
Comment
Watch Question

TracyVBA Developer

Commented:
Yes, you can do something along the lines of this:

'Set strProgLoc to the executable file that is being opened by the batch file
strProgLoc = "C:\Program Files\Monarch\program\MONARCH.EXE"
Shell strProgLoc, vbMaximizedFocus


Function Get_Data()

    Dim strRPTLoc       As String
    Dim strMODLoc       As String
    Dim strRPTName      As String
    Dim strProgLoc      As String
    
            
    'Monarch Import
    strProgLoc = "C:\Program Files\Monarch\program\MONARCH.EXE"
    
    Shell strProgLoc, vbMaximizedFocus 'open monarch
    
    Pause 5     'wait to open monarch
    
    SendKeys "^o"                   'Ctrl-O open file
    SendKeys strRPTLoc, True        'report name/location
    Pause 1     'wait to exit
    SendKeys "~", True              'Enter
    Pause 1     'wait to exit
    SendKeys "^o", True             'Ctrl-O open file
    SendKeys strMODLoc, True        'model name/location
    Pause 1     'wait to exit
    SendKeys "~", True              'Enter
    Pause 1     'wait to exit
    SendKeys "%w", True             'Window menu
    SendKeys "t", True              'table
    SendKeys "%e", True             'Edit menu
    SendKeys "s", True              'select all
    SendKeys "^c", True             'Ctrl-C copy
    
    Pause 2     'wait to copy
    
    SendKeys "%f", True             'file menu
    SendKeys "x", True              'Exit monarch
    
    Pause 1     'wait to exit

End Function

Public Function Pause(lngSeconds As Long)

Dim Start As Variant

Start = Timer                       ' Set start time.
Do While Timer < Start + lngSeconds
    DoEvents                        ' Yield to other processes.
Loop

End Function

Open in new window

Author

Commented:
I think I was on the right track, but the Sendkeys didn't seem to recognize the open primgram window.

This is what I have, it runs the batch and opens the program and the login/password bow is the Active window, but the senkeys do not populate it.

Sub OpenMyMonitor()
Dim RetVal3

RetVal3 = Shell("cmd /c" & "C:\MyMonitor\Monitor\startMonitorLinux.bat", vbMaximizedFocus)

SendKeys "USERID"
SendKeys "<Tab>"
SendKeys "PASSWORD"
SendKeys "<Enter>"

End Sub
TracyVBA Developer

Commented:
First thing is that "<Tab>" and "<Enter>" is not valid sendkeys syntax.

Use this instead:
TabKey = "{TAB}"
EnterKey  = "~"

Trying tabbing through your form until you get to the UserID field.  You can't just do this:
SendKeys "USERID"

For the syntax look here:
http://orlando.mvps.org/SendKeysMore.asp

Author

Commented:
When the MyMonitor program opens a pop up appears that asks for a name and password. The curser is already in the Name box so shouldn't be able to just:


Sendkeys "my name"  
and it should fill in the Name box?


 

Author

Commented:
Do I need to CreateObject for the MyMonitor PopUp?
TracyVBA Developer

Commented:
>>Sendkeys "my name"   and it should fill in the Name box
No, you can't just sent over text, you have to send of the code for that text.  As you can see at the link I provided sending an "M" would be this:  {VK 77}

>>Do I need to CreateObject for the MyMonitor PopUp?
If there's an available dll for it that you can reference, then you could do that.  I don't know the program, so I wouldn't know if there is one or not.  If you go this route, then you would not need send keys.  In this case, close this question, as I have already provided you an answer to your original question and open a new one on how to create and object for that application.

Author

Commented:
I tried:
SendKeys "{VK 77}"  and it produces Run-time error 5 Invalid procedure call or argument
TracyVBA Developer

Commented:
Yeah, I tried it with that too, and it didn't work for me either.

I found this example, that works with notepad.  You should be able to modify to your needs.

http://bytes.com/topic/access/answers/849035-sendkeys-another-application

Sub test()


   Dim retVal As Variant
   
   retVal = Shell("Notepad", vbNormalFocus)    'Run Notepad
   
   SendKeys "George W. Bush"           'Type George W. Bush in Notepad Window
   SendKeys "%{F4}", True              'Send ALT+F4 to close Notepad
   
   SendKeys "{ENTER}", True            'Yes at the Save Prompt
  
   SendKeys "George W. Bush.txt", True     'Type Filename to Save Document to
  
   SendKeys "%S"                       'Activates the Save Button
  


End Sub

Open in new window

Author

Commented:
I ran this code as is. It opens Notepad but Sendkeys "George W. Bush"  doesn't send the text to Notepad.
TracyVBA Developer

Commented:
hmmm... it works for me.

Try the attached workbook.

Book2.xls

Author

Commented:
still didn't work. could it be a differnece in versions or a missing reference in the library? I am using VB 6.5 and Excel 2003
VBA Developer
Commented:
This code is for VBA within Excel, not for Visual Basic 6.5.  In your question you asked for VBA and not VB, there is a difference.

Please close this question by accepting this comment 33039684 as it answers the original question posted and re-open a new question specifying a VB 6.5 solution and not a VBA solution.