Excel VBA controlling IE confirmation popup box
All, I am controlling an IE website programmed in .NET and I am having an issue with a IE confirmation pop up box that has to be answered before my VBA code will continue. You will not be able to access this web page because it in internal to my company.
Wesite Site code line I am having issues with:
HTML Code:
<input type="button" name="ctl00$ContentPlaceHolder1$btnAction" value="Update" onclick="if (!confirm('Are you sure you wish to take this action?')) return false;__doPostBack('ctl00$ContentPlaceHolder1$btnAction','')" id="ctl00_ContentPlaceHolder1_btnAction" />
</div>
I can control the input button fine with:
[VBA]
IeApp.Document.all.item("ctl00_ContentPlaceHolder1_btnAction").Click
[/VBA]
The issue is I get a popup box because of the below code which I have to manually click before VBA will continue:
HTML Code:
onclick="if (!confirm('Are you sure you wish to take this action?')) return false;__doPostBack('ctl00$ContentPlaceHolder1$btnAction','')" id="ctl00_ContentPlaceHolder1_btnAction" />
Is there a way in one statement to click the button and update the onclick IE confirmation popup box that appears?
Is there a way to work around this IE confirmation pop up box?
Dealing with Pop Up Windows
Your issue sounds like your ieApp object is loisng focus when the pop up occurs and you have no reference to the new window, and thus it's HTML in order to manipulate it's controls.
You need to declare your ieApp object "withevents" and also set up another one for the pop-up. Then you can monitor the primary ieApp's events for the appearance of the pop up, get a reference to it and do what you need in the pop up.
The code below will give you a framework to work within. Given the withevents declarations, you either need to put this code into a Sheet Module or create Class module that you instantiate and control it from. The with events declarations won't compile in a Standard Module.
[VBA]Option Explicit
'Reference to Microsoft Internet Controls
'Reference to Microsoft HTML Object Library as well
Private WithEvents ieApp As InternetExplorer
Private WithEvents iePopup As InternetExplorer
Private Sub ieApp_NewWindow2(ppDisp As Object, Cancel As Boolean)
'Event Handler to monitor your primary ieApp's New Window event
'When this event fires, start up a new instance of an IE object
Set iePopup = New InternetExplorer
'set the popup window to the new instance of your iePopup object
Set ppDisp = iePopup
End Sub
Public Sub getData()
Dim URL As String
URL = "http://xyz.com"
Set ieApp = New InternetExplorer
With ieApp 'go to your page
.Visible = True
.navigate URL
Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
'do what you have to do to get to the pop up window
ieApp.Document.all.Item("ctl00_ContentPlaceHolder1_btnAction").Click
'Wait for the pop up window
Do Until ie.ReadyState = READYSTATE_COMPLETE _
And Not ie.Busy _
And (Not iePopup Is Nothing)
DoEvents
Loop
While iePopup.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
'You should now have the ability to reference the pop up window directly
With iePopup
'Do what you want from pop up window
Debug.Print "Pop Up's URL is: " & .LocationURL
End With
'close Pop Up Window
iePopup.Quit
Set iePopup = Nothing
End If
'Continue on with your primary window ieApp...
Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
End With 'ieApp
ieApp.Quit
Set ieApp = Nothing
End Sub
[/VBA]