Alternative to Sendkeys in VB

I have a program that opens an application(titale EXTRA in the code attached), logs into the application, and then navgiates through the application to run 4 different reports.  The problem I have is I used send keys to login and navigate through the opened application.  Because of the sendkeys function I am not able to schedule the program and get it to work when I am not logged into the PC(from what I have read to use send keys the window has to maintain focus).  Is there another function outside of sendkeys that willl allow me to run the program when I am not logged on and if so how do I convert the sendkeys to it.
Imports Microsoft.VisualBasic.Devices
 
Module Module1
 
    Sub Main()
        Dim ProcID As Integer  ' Start the EXTRA
        Dim counter As Integer = 0
        Dim check As Boolean = True
 
        ProcID = Shell("EXTRA.exe", AppWinStyle.NormalFocus) 
        AppActivate(ProcID)
        My.Computer.Keyboard.SendKeys("WFA_DO_CP.edp{Enter}", True)
        System.Threading.Thread.Sleep(5000)
        My.Computer.Keyboard.SendKeys("na50{Enter}", True)
        System.Threading.Thread.Sleep(1000)
        My.Computer.Keyboard.SendKeys("loadload{Enter}", True)
        System.Threading.Thread.Sleep(2000)
        My.Computer.Keyboard.SendKeys("/for vgs1sign{enter}", True)
        System.Threading.Thread.Sleep(2000)
        My.Computer.Keyboard.SendKeys("loadload{Enter}", True)
        System.Threading.Thread.Sleep(1000)
        Do
            Do While counter < 10
                counter += 1
                If counter = 1 Then
                    AppActivate(ProcID)
                    My.Computer.Keyboard.SendKeys("{home}goqsre{enter}", True)
                    System.Threading.Thread.Sleep(1000)
                    My.Computer.Keyboard.SendKeys("{tab}132_dd{tab}vggdtldd{tab 2}ctr_supv", True)
                    My.Computer.Keyboard.SendKeys("%(T){enter}", True)
                    System.Threading.Thread.Sleep(1000)
                    My.Computer.Keyboard.SendKeys("OQS to Excel 2000{tab}{enter}", True)
                    System.Threading.Thread.Sleep(360000)
                ElseIf counter = 2 Then
                    AppActivate(ProcID)
                    System.Threading.Thread.Sleep(1000)
                    My.Computer.Keyboard.SendKeys("/for goqsre{enter}", True)
                    System.Threading.Thread.Sleep(1000)
                    My.Computer.Keyboard.SendKeys("{tab}731_dd{tab}vggdtldd{tab 2}ctr_supv", True)
                    My.Computer.Keyboard.SendKeys("%(T){enter}", True)
                    System.Threading.Thread.Sleep(1000)
                    My.Computer.Keyboard.SendKeys("OQS to Excel 2000{tab}{enter}", True)
                    System.Threading.Thread.Sleep(360000)
                ElseIf counter = 3 Then
                    AppActivate(ProcID)
                    System.Threading.Thread.Sleep(1000)
                    My.Computer.Keyboard.SendKeys("/for goqsre{enter}", True)
                    System.Threading.Thread.Sleep(1000)
                    My.Computer.Keyboard.SendKeys("{tab}132_ptd{tab}vggd1xdd{tab 2}ctr_supv", True)
                    My.Computer.Keyboard.SendKeys("%(T){enter}", True)
                    System.Threading.Thread.Sleep(1000)
                    My.Computer.Keyboard.SendKeys("OQS to Excel 2000{tab}{enter}", True)
                    System.Threading.Thread.Sleep(360000)
                ElseIf counter = 4 Then
                    AppActivate(ProcID)
                    System.Threading.Thread.Sleep(1000)
                    My.Computer.Keyboard.SendKeys("/for goqsre{enter}", True)
                    System.Threading.Thread.Sleep(1000)
                    My.Computer.Keyboard.SendKeys("{tab}731_ptd{tab}vggd1xdd{tab 2}ctr_supv", True)
                    My.Computer.Keyboard.SendKeys("%(T){enter}", True)
                    System.Threading.Thread.Sleep(1000)
                    My.Computer.Keyboard.SendKeys("OQS to Excel 2000{tab}{enter}", True)
                    System.Threading.Thread.Sleep(360000)
                    check = False
                    Exit Do
                End If
            Loop
        Loop Until check = False
    End Sub
 
End Module

Open in new window

wefarmer3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

obrienslalomCommented:
Try sendmessage w/ the appropriate vkey mappings
0
obrienslalomCommented:
[after writing these, i realized the tag said vbscript, and if i looked at the code closer, I would have realized that.  Anyway, I'm still going to post just in case you have the means to go a .net/vb 6/ native route /]
Before you try that, try sendinput .  I have a feeling this will work, but I've never tried it without being logged in.
You might want to avoid activating an application and start sending an arbitrary amount of stuff to it.  Especially when you have those sleeps in there.  Something could steal the focus...and well, unmonitored things could get awkward.  I would suggest getting the hwnd of the application and sending messages that way.  The messages should get queued up, avoid sleeps.  I will admit, things aren't going to look as pretty as the code you have there.
0
wefarmer3Author Commented:
I think that using the hwnd and sendinput function but not sure how to do that.  Can you please give a breif example of what that would look like based on the code above?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

obrienslalomCommented:
I don't think you can do what I was describing in vbscript.  Do you have the means to write it in something else?  If not, I'm not sure I know a fitting resolution.
0
wefarmer3Author Commented:
I have access to visual studio 2008 pro, so i can use any of the formats supported within it.
0
obrienslalomCommented:
Sorry it took me awhile to get back here.  I wrote this in vc++ since have access to the win32 functions without creating delegates etc.  Hopefully you have enough 'windows' structure in your application to pull this off.  I'm assuming you know how to use Spy++.  That is how I found the class names and title structure of the application.  I decided to show you how to do this within IE (although I forgot I have ie 8 beta, hopefully it correlates enough with whatever version you have).  As far as your code goes, if you have a predictable title in the title bar of your application, you can do this easily with findwindow.  This is what I used in the sample code below.  Otherwise you will have to use a combination of EnumWindows checking the pid of your spawned app through GetGUIThreadInfo in place of FindWindow.  That will give you the top level hWnd.  The rest of the controls should be consistant.

So here is the sample.  It just places a new url into the address bar and presses enter.  The commented out code refreshes the address display to show the text.  If you have buttons in your app, you can do SendMessage(btnHWnd,WM_LBUTTONDOWN,0,0) and WM_LBUTTONUP to click that bugger.  Hopefully these examples will be enough to get you the functionality you're looking for.  Let me know if you run into any problems and I'll try to help if I can.

HWND ie = FindWindow(L"IEFrame",NULL);
HWND navBar = FindWindowEx(ie,NULL,L"WorkerW",L"Navigation Bar");
HWND reBar = FindWindowEx(navBar,NULL,L"ReBarWindow32",NULL);
HWND add = FindWindowEx(reBar,NULL,L"Address Band Root",NULL);
HWND addressEdit = FindWindowEx(add,NULL,L"Edit",NULL);
 
SendMessage(addressEdit,WM_SETTEXT,0,(LPARAM)L"http://www.live.com");
SendMessage(addressEdit,WM_LBUTTONDOWN,0,0);
//just change the text w/o executing it
//InvalidateRect(addressEdit,NULL,TRUE); 
 
PostMessage(addressEdit,WM_KEYDOWN,VK_RETURN,0);
PostMessage(addressEdit,WM_KEYUP,VK_RETURN,0);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.