PDA

View Full Version : Automation of an system issue.



chacanger
01-06-2011, 04:14 PM
Hi

I'm currently trying to automate a program which works both in the format of an exe file and in the form of an aspx page. I have most of the code I need but there is a slight issue with getting VBA to press Enter and F12. I've tried many examples but they don't seem to work for my scenario.

I decided it was easier to use the aspx site rather than the exe file,
unfortunately I can't give a link to the aspx site. However it's kind of like a form were you input information, except that the other fields only appear after you complete the previous one and press enter, there is no buttons to press to proceed to the next field, so this is were I'm stuck trying to press Enter on Internet Explorer and F12 which is required at the end. Basiclly so far I can open Internet Explorer and type in the first message box and press buttons if required. Sorry it sounds a bit confusing.

I have managed to produce some code to replicate the same thing on Google and have put in a version for my aspx program without the link name to get the gist of how it looks. The input boxes all have the same name so in this example "p" in google is the Search Box were as in the aspx I need to type into is called TEXTINPUT.


Sub IE_Run_Google()
'----------------Google Example...

Dim ie As InternetExplorer
Set ie = New InternetExplorer

ie.Navigate "Google Website"
'Loop until it has loaded.
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop

ie.Visible = True
ie.AddressBar = False
ie.Toolbar = False

ie.Document.getelementbyID("q").Value = "VBA Example"
'---------------Needs to press enter here or F12

Set ie = Nothing

End Sub

Sub IE_Run_Myprogram()
'----------------How my ASPX Example works...

Dim ie As InternetExplorer
Set ie = New InternetExplorer

ie.Navigate "my aspx link"
'Loop until it has loaded.
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop

ie.Visible = True
ie.AddressBar = False
ie.Toolbar = False

ie.Document.getelementbyID("TEXTINPUT").Value = "OI"
'---------------Needs to press enter here
ie.Document.getelementbyID("TEXTINPUT").Value = "N"
'---------------Needs to press enter here
ie.Document.getelementbyID("TEXTINPUT").Value = "000000000"
'---------------Needs to press enter here
ie.Document.getelementbyID("TEXTINPUT").Value = "0000"
'---------------Needs to press enter here or F12

Set ie = Nothing

End Sub

Your help is much appreciated

stanleydgrom
01-06-2011, 09:05 PM
chacanger,

Depending on the application it may or may not work.

There are some applications where you actually have to know the cursor location on the screen for the input area, and you may also have to simulate pressing the ENTER key down.

And, there might be a timing difference with your application.

Good luck.


Your two separate examples:



ie.Document.getelementbyID("q").Value = "VBA Example"
'---------------Needs to press enter here or F12
SendKeys "{F12}", True


ie.Document.getelementbyID("TEXTINPUT").Value = "OI"
'---------------Needs to press enter here
SendKeys "{ENTER}", True




Have a great day,
Stan

chacanger
01-07-2011, 12:17 PM
Thanks for the reply stanleydgrom, I tried SendKeys before on this code, It seems to get past the first line ok, but then that's when it seems to prevent the next field from working correctly. I put True after the SendKeys function too but the it still has the same issue, I reckon it is the timing issues like you said, would it work if I were to use the DDE Execute method, if so do you know how I would be able to code that?

It's funny really, someone who worked in our place a while ago knew how to do it, but I can't figure out how they did it.:think:

Sean.DiSanti
01-10-2011, 12:01 PM
check out autoit. http://autoitscript.com/autoit3 it's a free, high level basic based programming language that makes working with all areas of the windows API pretty simple. including sending directly to any standard control. there's a great support forum over there also and feel free to pm me here or there (Cameronsdad is my name over there) if you go that route and have any questions

Bob Phillips
01-10-2011, 12:56 PM
check out autoit. http://autoitscript.com/autoit3

I agree with Sean, it is a fantastic piece of work, yet another free gem.

chacanger
01-11-2011, 06:23 AM
I agree with Sean, it is a fantastic piece of work, yet another free gem.

I can't use it unfortunately, were the computer I'm working on is situated, they don't allow any downloads or uploads. I did maaged to automate it further though, I did a Private Declaration for the system file Kernel32.dll to allow sleeps in between key presses. The issue I have now is that can't get it to find a certian piece iof text off the screen to paste into Excel.

I managed to do pasting into the ASPX Page with OLECMDPASTE, so I tried OLECMDFIND which brings the search up, but I need it to search for text without me needing to manually input a search string.

Any Ideas?:think:

Many Thanks

Chacanger

Kenneth Hobs
01-12-2011, 09:53 AM
Maybe you can use a spy program like patorjk.com to get a window title or class to know when to SendKeys()? That tool has a feature to generate code for API calls for finding windows or sending a button click or such. You may need to get a win32api.txt file or an API-Guide to get API code for the functions that patorjk shows.

API-Guide: http://allapi.mentalis.org/agnet/apiguide.shtml

Here is an example using the Sleep API and another method to sendkeys. That method does not require UAC to be turned off as SendKeys() does.

Declare Sub keybd_event Lib "user32" (ByVal bVk As Integer, ByVal bScan As Integer, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Declare Sub Sleep Lib "KERNEL32" (ByVal dwMilliseconds As Long)

' Constant declarations:
Const VK_RCONTROL = &HA3
Const VK_LCONTROL = &HA2
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Const VK_Tab = 9

Sub Main()
AppActivate "ODOT", True
SendKeys "umtr121", True
SendKeys "{Tab}", True
SendKeys "ken", True
'Simulate Key Press
keybd_event VK_RCONTROL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simulate Key Release
keybd_event VK_RCONTROL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End Sub

Sub KeyPress(key As Integer)
Call keybd_event(key, 0, 0, 0)
Sleep 100
Call keybd_event(key, 0, &H2, 0)
End Sub

I wrote a vb.net short application that one can sendkeys as input parameters without the need to turn of UAC.

chacanger
01-13-2011, 06:37 AM
Maybe you can use a spy program like patorjk.com to get a window title or class to know when to SendKeys()? That tool has a feature to generate code for API calls for finding windows or sending a button click or such. You may need to get a win32api.txt file or an API-Guide to get API code for the functions that patorjk shows.

Hey I've managed to get it work at last, the select all feature seems to be working on the program now. I also used a Declare Funtion to stop users from interupting the code until it finishes. It seems that USER32 has a BlockInput Feature that if you set to true prevents both mouse and keyboard interuptions, if you press Ctrl+Alt+Del it allows control too.

It seems that the Sleep from Kernal32 and BlockInput from User32 really helped in my situation.

:bow: Thanks for all your advice people.