Log in

View Full Version : IE Automation using VBA



kavkazi
10-20-2011, 08:04 AM
Hello All,

I'm successfully navigating to an Internet Explorer Web Page using Microsoft Internet Controls. I am able to login, click some buttons and hyperlinks, but then I get stuck on a Pop-Up message from the webpage that asks to confirm my selection (OK or Cancel).

I tried using sendkeys to hit enter, but the vba code stops as soon as it clicks on the link that brings this pop-up. So all the code after that is not executed.

Here is the code.


Dim IeApp As InternetExplorer
Dim sURL As String
Dim IeDoc As Object
Dim Element As HTMLLinkElement
Set IeApp = New InternetExplorer
IeApp.Visible = True
sURL = "Some URL Here"
IeApp.Navigate sURL
Do
Loop Until IeApp.ReadyState = READYSTATE_COMPLETE
Set IeDoc = IeApp.Document
For Each Element In IeDoc.Links
If Element.innerText = "Book Now" Then
Call Element.Click ' pop-up here. Code after this is not executed.
Exit For
End If
Next Element
SendKeys "{ENTER}", True

Is there any way I can do this? Please it is very important for me.

Thanks in advance.

kavkazi
10-20-2011, 09:36 AM
Just in case it would help, here is the source code of the website:

Here is the source code for the function:

function ConfirmBooking(courtno,bdate,btime,ctime,schedule_num)
{

var flag = confirm("Please confirm you are booking court "+courtno+" for the date 10/27/2011 at the time "+ctime);
if(flag == true)
{
document.book.Court_num.value = courtno;
document.book.Court_Date.value = bdate;
document.book.Court_Hours.value = btime;
document.book.ctime.value = ctime;
document.book.Schedule_Num.value = schedule_num;
document.book.submit();
}

}

Here is the call to the function:

<a href="#" onclick="ConfirmBooking('1','2011-10-27 00:00:00','1899-12-30 13:00:00','1:00 pm','2503');">1:00 pm</a> </td></tr>
<tr><td align="center" class="booktime">

mohanvijay
10-20-2011, 08:02 PM
I am using use below step to click the "open" button in open file dialog in IE automation maybe this will help

you can do it by Sendmessage API Call

1. Find hwnd of pop up menu by Findwindow API
2. loop through childwindows of Pop Up window get the hwnd of "Ok" button by FindwindowEx API
3.click Ok button by Sendmessage API

kavkazi
10-20-2011, 08:15 PM
Thanks very much for your reply.

I found this code, is this what you mean?


Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const VK_SPACE = &H20
Dim x As Long
Dim button As Long

Private Sub CommandButton1_Click()
Dim i As Integer
Do While i < 100
i = i
DoEvents
Sleep (1000)
x = FindWindow("#32770", "Microsoft Internet Explorer")
button = FindWindowEx(x, 0&, "button", "OK")
Call PostMessage(button, WM_KEYDOWN, VK_SPACE, 0&)
Call PostMessage(button, WM_KEYUP, VK_SPACE, 0&)
Call PostMessage(button, WM_KEYDOWN, VK_SPACE, 0&)
Call PostMessage(button, WM_KEYUP, VK_SPACE, 0&)
DoEvents
Loop
End Sub

How would I call it when using the call element.click in my code


Dim IeApp As InternetExplorer
Dim sURL As String
Dim IeDoc As Object
Dim Element As HTMLLinkElement
Set IeApp = New InternetExplorer
IeApp.Visible = True
sURL = "Some URL Here"
IeApp.Navigate sURL
Do
Loop Until IeApp.ReadyState = READYSTATE_COMPLETE
Set IeDoc = IeApp.Document
For Each Element In IeDoc.Links
If Element.innerText = "Book Now" Then
Call Element.Click ' pop-up here. Code after this is not executed.
Exit For
End If
Next Element

mohanvijay
10-20-2011, 08:46 PM
try this


'declere API function to send messages to window
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long

'declere API function to find in child windows

Private Declare Function FindWindowEx Lib "user32" Alias _
"FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

'declere API function to find window

Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Const BM_CLICK = &HF5

Sub Your_Procedure()
Const Dialog_Caption As String = "Your Caption"
Const Button_ClassName As String = "BUTTON" ' Assumes class name is BUTTON if error accures you can find class name by getcalssname API
Const ButtonTxt As String = "&OK" '"&" means underscore under text 'O'
Dim WIN_Dialog As Long 'Hold dialogbox hwnd
Dim Dlg_ChildWIN As Long 'hold hwnd of childwindows of dialogbox
Dim IeApp As InternetExplorer
Dim sURL As String
Dim IeDoc As Object
Dim Element As HTMLLinkElement
Set IeApp = New InternetExplorer
IeApp.Visible = True
sURL = "Some URL Here"
IeApp.Navigate sURL
Do
Loop Until IeApp.ReadyState = READYSTATE_COMPLETE
Set IeDoc = IeApp.Document
For Each Element In IeDoc.Links
If Element.innerText = "Book Now" Then
Call Element.Click ' pop-up here. Code after this is not executed.
WIN_Dialog = FindWindow(vbNullString, Dialog_Caption) 'get the dialogbox hwnd
If WIN_Dialog = 0 Then
MsgBox """" & Dialog_Caption & """ - Window not found"
Exit Sub
End If
Dlg_ChildWIN = FindWindowEx(WIN_Dialog, 0, Button_ClassName, ButtonTxt)

If Dlg_ChildWIN = 0 Then
MsgBox "Ok Button not found in """ & Dialog_Caption & """ Window"
Exit Sub
End If
SendMessage Dlg_ChildWIN, BM_CLICK, 0, 0 'click button
Exit For
End If
Next Element
End Sub

kavkazi
10-20-2011, 09:06 PM
The problem is when Call Element.Click is called, the code after that is not executed. It waits for the user to either click ok or cancel. Then it continues.

To test, I put a msgbox "test" right after the Call Element.Click, and it is not called. When I press cancel in the element box then the rest of the code is executed and it says "Dialog caption window not found" because I already closed it.

mohanvijay
10-20-2011, 09:16 PM
did you change the following const value as pop-up menu caption



Const Dialog_Caption As String = "Your Caption"

kavkazi
10-21-2011, 06:04 AM
yes I did. The code pauses at this line



Call Element.Click ' pop-up here. Code after this is not executed.


Please help me, thanks

myers_co
01-21-2012, 05:45 AM
try this


'declere API function to send messages to window
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long

'declere API function to find in child windows

Private Declare Function FindWindowEx Lib "user32" Alias _
"FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

'declere API function to find window

Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Const BM_CLICK = &HF5

Sub Your_Procedure()
Const Dialog_Caption As String = "Your Caption"
Const Button_ClassName As String = "BUTTON" ' Assumes class name is BUTTON if error accures you can find class name by getcalssname API
Const ButtonTxt As String = "&OK" '"&" means underscore under text 'O'
Dim WIN_Dialog As Long 'Hold dialogbox hwnd
Dim Dlg_ChildWIN As Long 'hold hwnd of childwindows of dialogbox
Dim IeApp As InternetExplorer
Dim sURL As String
Dim IeDoc As Object
Dim Element As HTMLLinkElement
Set IeApp = New InternetExplorer
IeApp.Visible = True
sURL = "Some URL Here"
IeApp.Navigate sURL
Do
Loop Until IeApp.ReadyState = READYSTATE_COMPLETE
Set IeDoc = IeApp.Document
For Each Element In IeDoc.Links
If Element.innerText = "Book Now" Then
Call Element.Click ' pop-up here. Code after this is not executed.
WIN_Dialog = FindWindow(vbNullString, Dialog_Caption) 'get the dialogbox hwnd
If WIN_Dialog = 0 Then
MsgBox """" & Dialog_Caption & """ - Window not found"
Exit Sub
End If
Dlg_ChildWIN = FindWindowEx(WIN_Dialog, 0, Button_ClassName, ButtonTxt)

If Dlg_ChildWIN = 0 Then
MsgBox "Ok Button not found in """ & Dialog_Caption & """ Window"
Exit Sub
End If
SendMessage Dlg_ChildWIN, BM_CLICK, 0, 0 'click button
Exit For
End If
Next Element
End Sub


Which reference (library) includes the HTMLLinkElement?

Crocus Crow
01-24-2012, 05:13 PM
Which reference (library) includes the HTMLLinkElement?Microsoft HTML Object Library.

However the code should declare the Element variable as a HTMLAnchorElement because IeDoc.Links is a collection of HTMLAnchorElements (A tags). HTMLLinkElement is associated with the LINK tag for linking style sheets.