stefanpantu
asked on
VB6/VBA Autologin to IE 6 Web Page With Frames
Group,
Using VBA
I am trying to automatically log on to a web page that has two frames. The HTML for the logon is on the right hand frame. Right click on the right of the page at http://www.alaron.ranweb.com/login/alr.asp and select View Source to see the HTML/frame code.
I assume that the logon HTML code is in form 1 (Forms(1)?) but I am not sure. Logically, the left frame would be Forms(0) and the right Frame is Forms(1).
I have tried using SendKeys but it does not seem to work with framed web pages.
Here is the function:
Option Explicit
Function TestLogonIE() As Boolean
'test to logon to an IE web page
Dim IE As Object
Dim hwnd As Long
Dim oForm As Object
Set IE = CreateObject("InternetExpl orer.Appli cation")
IE.Navigate "http://www.alaron.ranweb.com/login/alr.asp"
IE.Visible = True
Do While IE.Busy And IE.ReadyState <> 4
DoEvents 'wait until IE is done loading page.
Loop
hwnd = IE.hwnd
SetFocusToBrowser (hwnd) 'API call to set focus to newly opened browser window, not sure if required, can be deleted?
' *** the code below does not work
Set oForm = IE.Document.Forms(1)
oForm.Elements("Username") .Value = "username"
oForm.Elements("Password") .Value = "password"
oForm("AnchorLogOn").Click
End function
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Declare Function SetFocusAPI Lib "user32" _
Alias "SetFocus" (ByVal hwnd As Long) As Long
Declare Function GetFocus Lib "user32" () As Long
Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Long) As Long
'GetWindow constants
Public Const GW_CHILD = 5
'GetWindowLong constants
Public Const GWL_STYLE = (-16)
Public Const WS_VSCROLL = &H200000
Sub SetFocusToBrowser(hBrowser Hwnd As Long)
Dim lStyle As Long
Dim lResult As Long
Dim hwnd As Long
hwnd = hBrowserHwnd
While (lResult = 0) And (hwnd <> 0)
hwnd = GetWindow(hwnd, GW_CHILD)
lStyle = GetWindowLong(hwnd, GWL_STYLE)
lResult = lStyle And WS_VSCROLL
Wend
SetFocusAPI (hwnd)
End Sub
Using VBA
I am trying to automatically log on to a web page that has two frames. The HTML for the logon is on the right hand frame. Right click on the right of the page at http://www.alaron.ranweb.com/login/alr.asp and select View Source to see the HTML/frame code.
I assume that the logon HTML code is in form 1 (Forms(1)?) but I am not sure. Logically, the left frame would be Forms(0) and the right Frame is Forms(1).
I have tried using SendKeys but it does not seem to work with framed web pages.
Here is the function:
Option Explicit
Function TestLogonIE() As Boolean
'test to logon to an IE web page
Dim IE As Object
Dim hwnd As Long
Dim oForm As Object
Set IE = CreateObject("InternetExpl
IE.Navigate "http://www.alaron.ranweb.com/login/alr.asp"
IE.Visible = True
Do While IE.Busy And IE.ReadyState <> 4
DoEvents 'wait until IE is done loading page.
Loop
hwnd = IE.hwnd
SetFocusToBrowser (hwnd) 'API call to set focus to newly opened browser window, not sure if required, can be deleted?
' *** the code below does not work
Set oForm = IE.Document.Forms(1)
oForm.Elements("Username")
oForm.Elements("Password")
oForm("AnchorLogOn").Click
End function
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Declare Function SetFocusAPI Lib "user32" _
Alias "SetFocus" (ByVal hwnd As Long) As Long
Declare Function GetFocus Lib "user32" () As Long
Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Long) As Long
'GetWindow constants
Public Const GW_CHILD = 5
'GetWindowLong constants
Public Const GWL_STYLE = (-16)
Public Const WS_VSCROLL = &H200000
Sub SetFocusToBrowser(hBrowser
Dim lStyle As Long
Dim lResult As Long
Dim hwnd As Long
hwnd = hBrowserHwnd
While (lResult = 0) And (hwnd <> 0)
hwnd = GetWindow(hwnd, GW_CHILD)
lStyle = GetWindowLong(hwnd, GWL_STYLE)
lResult = lStyle And WS_VSCROLL
Wend
SetFocusAPI (hwnd)
End Sub
ASKER
D'Al
Thanks for your toughtful response. The last lines you added worked, but the final line
ie.Document.frames(1).Docu ment.forms (0).submit
does not work, it sends me to an alternate logon page.
I'm guessing that the ...submit code does not work is because when the Logon button is clicked the following javascript code is executed:
javascript:savePWD();b("Lo gOn",%20"r anWeb.asp? ranApplica tion=stati c&ranMenu= intro&stat icid=intro &clearCrit eria=true& applayouti d=203&acti onid=377&r anButton=L ogOn&run=1 25")
For some reason, the Logon button is not a named object so code like
IE.Document.frames(1).Docu ment.forms (0).elemen ts("Logon" ).Click won't work
Also, I saw that the Naviagate command has a submit parameter
see http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/methods/navigate.asp
Could this be used to fool the ie.Document.frames(1).Docu ment.forms (0).submit code to submit the javascript code?
If not, any suggestions to simulate clicking the logon button?
Thanks,
Stefan
Thanks for your toughtful response. The last lines you added worked, but the final line
ie.Document.frames(1).Docu
does not work, it sends me to an alternate logon page.
I'm guessing that the ...submit code does not work is because when the Logon button is clicked the following javascript code is executed:
javascript:savePWD();b("Lo
For some reason, the Logon button is not a named object so code like
IE.Document.frames(1).Docu
Also, I saw that the Naviagate command has a submit parameter
see http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/methods/navigate.asp
Could this be used to fool the ie.Document.frames(1).Docu
If not, any suggestions to simulate clicking the logon button?
Thanks,
Stefan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Enjoy your points.
Could I trouble you for a good link or book that explains the IE object model in greater detail?
Thanks,
Stefan
Could I trouble you for a good link or book that explains the IE object model in greater detail?
Thanks,
Stefan
No trouble at all - just check MSDN...
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/objects/internetexplorer.asp
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/objects/internetexplorer.asp
'test to logon to an IE web page
Dim IE As Object
Dim hwnd As Long
Dim oForm As Object
Set IE = CreateObject("InternetExpl
IE.Navigate "http://www.alaron.ranweb.com/login/alr.asp"
IE.Visible = True
Do While IE.Busy And IE.ReadyState <> 4
DoEvents 'wait until IE is done loading page.
Loop
ie.Document.frames(1).Docu
ie.Document.frames(1).Docu
ie.Document.frames(1).Docu
ie.Document.frames(1).Docu
Hope This Helps,
D'Al