Solved

VB6/VBA Autologin to IE 6 Web Page With Frames

Posted on 2004-09-17
5
958 Views
Last Modified: 2011-09-20
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("InternetExplorer.Application")
 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(hBrowserHwnd 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


0
Comment
Question by:stefanpantu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:samopal
ID: 12089288
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("InternetExplorer.Application")
 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).Document.forms(0).elements("Username").value="Login"
ie.Document.frames(1).Document.forms(0).elements("Password").value="Pwd"
ie.Document.frames(1).Document.forms(0).elements("chkPSWD").click  'Optional, to check checkbox
ie.Document.frames(1).Document.forms(0).submit

Hope This Helps,
D'Al
0
 

Author Comment

by:stefanpantu
ID: 12098948
D'Al

Thanks for your toughtful response.  The last lines you added worked, but the final line

ie.Document.frames(1).Document.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("LogOn",%20"ranWeb.asp?ranApplication=static&ranMenu=intro&staticid=intro&clearCriteria=true&applayoutid=203&actionid=377&ranButton=LogOn&run=125")

For some reason, the Logon button is not a named object so code like

IE.Document.frames(1).Document.forms(0).elements("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).Document.forms(0).submit code to submit the javascript code?

If not, any suggestions to simulate clicking the logon button?

Thanks,

Stefan


0
 
LVL 9

Accepted Solution

by:
samopal earned 500 total points
ID: 12103512
You are right - Logon button is not a real button, it is an image. Use this line to simulate click :

IE.Document.frames(1).Document.images("ImageLogOn").click


D'Al
0
 

Author Comment

by:stefanpantu
ID: 12103863
Enjoy your points.

Could I trouble you for a good link or book that explains the IE object model in greater detail?  

Thanks,

Stefan
0
 
LVL 9

Expert Comment

by:samopal
ID: 12104203
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question