PDA

View Full Version : sendkeys question



vzachin
02-11-2009, 07:49 PM
hi,

i'm need to access an intranet website which requires me to navigate 3 screens before i get to the screen to input data and evoke a query.

i can access the site but i'm having trouble with the 1st screen. there's a button that says LOGIN that i can click or i can tab 2 times and hit the enter key.

i don't know how to evoke a button click so i tried using SendKeys but i can't seem to get that to work.

this is from the source code:

<center><a href="InitialLogin.jsp">Login</a></center>
</form> </div>


here's my code so far
Sub Intranet()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
On Error GoTo 1
With IE
.Navigate "http://myintranetsite.jsp"
.Visible = True

AppActivate "Internet Explorer"
SendKeys ("{TAB}")
SendKeys ("{TAB}")
SendKeys ("{ENTER}")

End With

Set IE = Nothing
Exit Sub


1: MsgBox Err.Description
IE.Quit
Set IE = Nothing

End Sub


this will be the first of a few more questions one step at a time


thanks
zach

Kenneth Hobs
02-14-2009, 10:16 AM
It is best to avoid Sendkeys. If you are using Vista, you will need to disable UAC for SendKeys to work.

It would be better to use the internet explorer's object model to Submit the form rather than Sendkeys. You can iterate the doc's All collection and get the control for the "form" and submit it.

In either case, notice how in the code, a loop makes the code wait until the page has loaded.
'Norie, http://www.ozgrid.com/forum/showthread.php?t=69569
' http://www.mrexcel.com/board2/viewforum.php?f=2
Sub Test()
Dim lnk As Object, ie As Object, doc As Object, i As Long
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate "http://www.mrexcel.com/board2/viewforum.php?f=2"
Do Until .readyState = 4: DoEvents: Loop

Set doc = ie.document


For Each lnk In doc.Links
If lnk.Classname = "topictitle" Then
i = i + 1

Range("A" & i) = lnk.innertext
Range("B" & i) = lnk

End If
Next lnk

End With

End Sub