How to automate Internet Explorer confirmation box

niteblind

New Member
Joined
Apr 9, 2011
Messages
8
Hi,

Hope you can help with this infuriating issue.

I have a ASP web page one section of which allows me to remove certain data from the page by click a "Close" link. The link is a straight forward HTML affair, and I can automate the clicking of it, however, it comes up with a confirmation box with an OK and Cancel button on it. My code then hands until I click OK.

I had seen a previous post on here advising to change the attribute of the button but that comment related to a Javascript settup which this is not.

The only solutions I can find when I am googling are relating to using Sendkeys, which NEVER works for me. Are there any alternatives?

My Current code is as follows, but always dies as soon as I hit the a.click statement:

Code:
Sub test() 
    Dim appIE As InternetExplorer 
    Dim sURL As String 
    Dim ElementCol As MSHTML.IHTMLElementCollection
 
Application.ScreenUpdating = False 
Set appIE = New InternetExplorer 
sURL = "http:// ..."

With appIE 
  .Navigate sURL 
  .Visible = True 
End With 
                                
Do While appIE.Busy 
Loop 

'NEXT LOOOP NEEDED FOR THE ASP PAGE TO LOAD  PROPERLY                            
timedelay = Now() 
Do Until (Now() > timedelay + TimeValue("00:00:05")) 
Loop 
     
Set ElementCol = appIE.Document.getElementsByTagName("A") 
For Each a In ElementCol 
  Select Case a.ID 
    Case Is = "Close" 
      a.Click 
      Exit For 
  End Select 
Next
I have a similar issue with another automation that needs to download a file, I do not know there either how to click the Open button on te standard IE message box, I assume the solutions will be similar for bother issues???

Any assistance would be greatly appreciated!

niteblind
 
Last edited:
Hi,

Thanks for the reply, all points taken on board, however, the reason I was asking about a better reference, was as an example ...

I found a reference to MSHTML.HTMLDialog and the reference toells me there is a Close method,

I do not know though how to get that into the code, I can figure out

Code:
Dim box As MSHTML.HTMLDialog
but then what, how to I tell the code I mean that dialog box?

Code:
For each box in IE.Dialog?????
The was as far as I could see also nothing saying how to close it with the OK or Cancel button or would it be box.Close = True?

I realise this is not the basic stuff but I canot find anywhere that will guide on the basics of working through the references and how to use them.

Thanks again though.
Andy
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I've no idea if HTMLDialog is appropriate for your situation, but the bit you are missing is IE.HTMLDocument, which is the DOM of the web page. Code to reference and access HTMLDocument is something like this (incomplete and untested code, and probably not appropriate to your situation but it shows the basics):
Code:
Dim HTMLdoc as HTMLDocument
Dim tables As IHTMLElementCollection
Dim table As HTMLTable
Dim link As HTMLAnchorElement

Set HTMLdoc = IE.HTMLDocument
Set tables = HTMLdoc.GetElementsByTagName("TABLE")
For each table In tables
  debug.print table.id
Next
For Each link In HTMLdoc.links
  debug.print link.href
Next
For IHTMLDialog, see http://msdn.microsoft.com/en-us/library/aa752691(VS.85,loband).aspx, which mentions ShowModalDialog (http://msdn.microsoft.com/en-us/library/aa741502(VS.85).aspx), which is part of IHTMLWindow2 (http://msdn.microsoft.com/en-us/library/aa741505(VS.85).aspx). I've never used IHTMLDialog nor ShowModalDialog, so I've no idea if they are appropriate to your situation.
 
Upvote 0
Possibley a little off topic, however, I ws on the MSDN and found this:

Code:
 [B]Private Sub[/B] [I]object[/I]_[B]FileDownload[/B]( _
	[B]ByVal[/B] [I]bActiveDocument[/I][B] As [B]Boolean[/B][/B][B],[/B] _
	[B]ByRef[/B] [I]bCancel[/I][B] As [B]Boolean[/B][/B])
</pre>This is VB code from what I am reading but is there a VBA equiv?
 
Upvote 0
Where did you see that code? It looks like event code.
There doesn't actually seem to be anything in the code itself that couldn't be done in VBA.
I think the context and what object(s) it applies to are probably more important.
 
Upvote 0
Hi,

I found the code on the MSDN, if I were to do this in VBA, how would I go about it???

Also I tried the code that was post early to scroll through the shell windows:
Code:
Dim Shell As Object, IE As Object
    
    Set Shell = CreateObject("Shell.Application")
    For Each IE In Shell.Windows
        Debug.Print TypeName(IE.Document), IE.LocationURL
        If TypeName(IE.Document) = "HTMLDocument" Then
            Debug.Print IE.LocationName, IE.LocationURL
            ActiveSheet.Range("A1").Value = IE.LocationName
            ActiveSheet.Range("A2").Value = IE.LocationURL
        End If
    Next

How would I ouput all open shell windows and their title bar as an example? I only ask as each time I try and tweak it, it breaks, however, when it runs fine it only shows internet explorer windows.

I am asking as I have 2 internet explorer windows open, one of which has the pop window on it, however, although it says Microsoft Internet explorer at the top of the pop up box but there is no separate instance of it on the toolbar. As a result I thought I would check all the shell windows and see if it appears anywhere else. Being I also had notpad and a few other appliactions open I expected to see more than 2 shell winows but it does not. I even tried changing the IE Object to Sh in case it was picking somethign up from that and it makes no difference.

Boy does this stuff make me feel dumb :)
 
Upvote 0
What code are you referring to?

If it's what you found earlier I don't think you can use it in VBA, at least not for what you want to do.

Like I said it's event code for an object, it applies to only a couple of things but I can't remember what they were off the top of my help.

I think one of them was something to do with a browser control.

Anyway all that event does is tell you whether or not it's the active document being downloaded and allows you to stop that if you want.

What is it you want to do exactly?

Perhaps it's not as hard as you think it is, you might even be overcomplicating it a bit.

It is hard to tell though and hard to help without a URL.:)
 
Upvote 0

Forum statistics

Threads
1,223,277
Messages
6,171,148
Members
452,382
Latest member
RonChand

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top