In my VBA Excel application, I need to use Internet Explorer to obtain some required data. From my VBA program, I also need to be able to set values in the Internet Explorer within several textboxes, select a row in a listbox, and Click on a button all within Internet Explorer.
1. I activated the Internet Explorer using the following code:
(Credit for this goes to leonstryker)
'=========================
==========
==========
==========
======
Dim IE
Dim objShell
Set IE = CreateObject("InternetExpl
orer.Appli
cation")
Set objShell = CreateObject("WScript.Shel
l")
With IE
.Left = 20
.Top = 20
.Height = 540
.Width = 950
.MenuBar = 0
.Toolbar = 1
.StatusBar = 0
.navigate "
http://www.google.com"
.Visible = True
End With
'wait until IE has finished loading itself.
Do While IE.busy
DoEvents
Loop
' The Internet Explorer is now hopefully ready to accept commands/values or clicks.
Stop
'=========================
==========
==========
==========
==========
==========
===
Since I do not know the names of All of the controls in all cases, I put together the following code to find all of the controls on a dummy form I created. It works fine on this dummy form, but what I really want is to be able to find all of the controls within the Internet Explorer. How do I make it look at the Internet Explorer Form rather than my dummy form?
'=========================
==========
==========
==========
==========
==========
===
Private Sub FindControls()
Dim Top As Integer
Dim Left As Integer
Dim Count As Integer
Dim Name As String
Dim Value As String
Dim temp As String
Dim Caption As String
Dim Enabled As Boolean
Dim Visible As Boolean
Dim FormControl As Control
' Find all of the controls on a VBA form. See VBA Help on "Controls Collection (Forms)"
Count = Controls.Count ' Total number of controls on Form
For Each FormControl In Controls
Name = FormControl.Name ' get the name. ie. btn..., txt..., lst... etc.
Top = FormControl.Top ' Get its Top Location
Left = FormControl.Left ' get its Left location
temp = Mid(Name, 1, 3) ' Get first 3 characters of control name
' The 4 controls below don't have values.
If temp = "frm" Or _
temp = "lbl" Or _
temp = "lst" Or _
temp = "Img" Then
Else
Value = FormControl.Value ' Get current value in the control
End If
Visible = FormControl.Visible ' T/F is it visible
Enabled = FormControl.Enabled ' T/F is it enabled
Next
End Sub
'=========================
==========
==========
==========
==========
==========
=========
At this point I need to be able to place a text string in one or more textboxes, click on a row in a listbox and then programatically initiate an click_Event on a button. This has to be done FROM within my VBA code TO the controls within the Internet Explorer.
Is anyone aware of any good books or articles that address how to manipulate Internet Explorer from witin a VBA program? So far I've had only marginal luck searching for information on this topic.
John
Start Free Trial