• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3252
  • Last Modified:

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

0
wefarmer3
Asked:
wefarmer3
  • 4
  • 2
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now