PDA

View Full Version : Code for detecting when a Msg box opens



bconner
04-20-2009, 05:38 AM
I have a program that is retrieving information from a website and dropping it back into Excel. Periodically a Msg box will pop up from the website, what is the code for detecting when a Msg box has popped up? I want to send key {Enter} when the msg box pops up.

Oorang
04-20-2009, 07:33 AM
You can do it that way, but you have to hit the windows API to do it. Probably easier to A.) Learn the conditions that trigger the box and then monitor said conditions. B.) Save a copy of the web page and modify the code so it doesn't pop up messages any more.

bconner
04-20-2009, 08:27 AM
Hi Aaron,
The website I am accessing is a Medicaid (Gov) website so I can't alter the webpage. How do I hit the windows API to determine when a msg box has popped up?

Kenneth Hobs
04-20-2009, 09:32 AM
What program do you have? It should handle any issues.

Most here would probably use the Microsoft Internet Explorer Object model to work with web sites using vba code. If we don't know the details, it would be hard to help.

Oorang
04-20-2009, 10:58 AM
AFAIK ShDocVw does not expose a method to detect and close a msgbox. Against my better judgement I played around with an API aproach but it would appear the alert method of JavaScript does not launch the dialog window as a child of IE. So you I ended up searching for windows from the desktop down, which is pretty slow, or you can find by title, but that's a little brittle. I don't have time to do a full solution but here is some junk code for you to play around with and see if it gives you any ideas, justbe careful play with it in a junk file and save early and often:
Option Explicit

Private Const WM_CLOSE As Long = &H10&
Private Const WM_GETTEXT As Long = &HD&

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) 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 GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
ByVal hwnd As Long, ByVal lpString As String, ByVal cch 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 EnumChildWindows Lib "user32" ( _
ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" ( _
ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Public Declare Function GetDesktopWindow Lib "user32" () As Long

Private m_lngChildren() As Long
Private m_lngIndx As Long
Private m_lngIndx2 As Long
Private m_lngUprBnd As Long
Private Sub Example()
Dim hwnd As Long
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
ie.Navigate "www.google.com"
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
ie.Navigate "javascript:alert(""I am an alert box!"");"
m_lngIndx = 0&: m_lngIndx2 = 1&: m_lngUprBnd = 100000
ReDim m_lngChildren(m_lngUprBnd)
m_lngChildren(m_lngIndx) = ie.hwnd 'GetDesktopWindow
Do While m_lngChildren(m_lngIndx) <> 0&
EnumChildWindows m_lngChildren(m_lngIndx), AddressOf EnumChildWindow, 0&
m_lngIndx = m_lngIndx + 1&
If m_lngIndx > m_lngUprBnd Then
m_lngUprBnd = m_lngUprBnd + 100000
ReDim Preserve m_lngChildren(m_lngUprBnd)
End If
Loop
ie.Quit
End Sub

Private Function EnumChildWindow(ByVal child As Long, ByVal param As Long) As Long
Dim strClass As String * 64&
Dim strText As String * 256&
Dim lngLen As Long
lngLen = GetClassName(child, strClass, 63&)
strClass = Left$(strClass, lngLen)
Debug.Print m_lngIndx, strClass
m_lngChildren(m_lngIndx2) = child
m_lngIndx2 = m_lngIndx2 + 1&
'EnumChildWindows child, 0&, 0&
'lngLen = SendMessage(child, WM_GETTEXT, 255&, strText)
'strText = Left(strText, lngLen)
EnumChildWindow = 1& ' Continue enumeration
End Function


For the record, my official recomendation is to:
1.) Make a local copy of the page.
2.) Modify all referential links to be explicit (on the live website).
3.) Repoint links to js files to local copies.
4.) Find and eliminate the code that launches the alert.