sendkeys to a launched IE browser

J

John

Code is simple... and there is no error in code (that VBA discerns) but the
sendkeys commands do not move the cursor in the new IE window and doesn't
send the active cell value either. Any thoughts?

Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie

.Visible = True

.navigate "http://whatever.com"
.resizable = True
End With

'App.Activate "Microsoft Internet Explorer"
SendKeys "{TAB}"
SendKeys ActiveCell.Value
SendKeys "~"
 
B

Bill Pfister

John, are you trying to fill in a form on a webpage or send a command to the
browser itself (such as "open page www.123.com)?

If you are trying to fill in a form on a page, my first guess is that you
aren't waiting long enough for the page to fully load. With the following
code I was able to browse to www.funny.com and enter "test" into the search
box. Sendkeys is very finicky and I would only use it as a last resort. If
the web page orientation (layout, tab-index, etc.) ever changes, your code
will break.

Regards,
Bill




Public Sub Test_IESendkeys()
Dim i As Long
Dim ie As Object

Set ie = CreateObject("InternetExplorer.Application")
With ie

.Visible = True

.navigate "http://www.funny.com"
.resizable = True
End With

Application.Wait (Now + TimeValue("0:00:10"))

'App.Activate "Microsoft Internet Explorer"
For i = 1 To 15
SendKeys "{TAB}"
Next i

SendKeys "test"
'SendKeys "~"
End Sub
 
J

John

Thanks for the response Bill... it was a time thing... I am trying to launch
a new window and then dump a cell value into a search on a website. This
works by adding that wait command...

I realize that if the website changes the code is no good... is there a way
around that?
 
R

Randy Harmelink

Rather than use SendKeys, you should be interacting with the web page
directly. For example, something like:

Set oIE = New InternetExplorer
oIE.Visible = True
oIE.Navigate Range("sURL")
Do: DoEvents: Loop Until oIE.ReadyState = READYSTATE_COMPLETE

Set oForm = oIE.Document.forms(0)
oForm("name1").Value = "Value1"
oForm("name2").Value = "Value2"
oForm("submitname").Click
Do: DoEvents: Loop While oIE.Busy
Do: DoEvents: Loop Until oIE.ReadyState = READYSTATE_COMPLETE

Set oForm = oIE.Document.forms(0)
oForm("cancelname").Click
 
B

Bill Pfister

Firstly, let me disclaim my reference to www.funny.com. I have no
accociation with the site - it just happened to be the site that popped up
when I entered "whatever.com".

If the objects on the page are tagged, you can get a better "grip" when
you're trying to access them. You can find out by looking at the page source
and try to find a name property. In the "funny.com" site, you'll see that
the search field has a name: <input class="formItem" size="10" type=text
value="" name="0.7.31.1.3">. You can "tie" to this object with more
elaborate code. You will still be at the mercy of the site designers if they
change the control name, but it is considerably more reliable than using the
tab index.

I haven't written code tie directly to an object embedded in a webpage,
although I know sample code is out there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top