PDA

View Full Version : Excel VBA controlling IE confirmation popup box



kj2686
06-24-2011, 11:22 AM
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:


<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:

IeApp.Document.all.item("ctl00_ContentPlaceHolder1_btnAction").Click


The issue is I get a popup box because of the below code which I have to manually click before VBA will continue:


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?

Shred Dude
06-25-2011, 07:32 PM
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.

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

Raygar
12-05-2012, 08:15 PM
I have a similar scenario.
I'm getting a popup window (Not a seperate IE window but a windows Ok/Cancel prompt). I can't use SendKeys because Ok/Cancel prompts pauses all code.

Here is a button that I'm trying to click or bypass.

<INPUT
style="WIDTH: 100px" id= StatusBar_btnDelete class=MyButton
onclick="DeleteBtnClicked(); return false;" value=Delete type=submit name=
StatusBar$btnDelete>











I should note that “DeleteBtnClicked()” is a function that does not resides in the immidate coding of this page. I can't see what it does.

I had done something like this previously and it worked, but not in this case.


Set
VoidButton = ie.document.getElementById("StatusBar_btnDelete")




ie.document.getElementById("StatusBar_btnDelete").removeAttribute("onclick")




ie.document.getElementById("StatusBar_btnDelete").setAttribute "onclick",
"Return True"



VoidButton.Click



Is there a way to deal with the active popup or change the click behavior?

mohanvijay
12-05-2012, 11:29 PM
Try this

http://vbaexpress.com/kb/getarticle.php?kb_id=1146

Raygar
12-06-2012, 06:26 PM
Thanks that worked, though it's a hefty comprimise.
I'll need to have a dedicated computer to run this robot job due to the popups that will occur every 20+ seconds. Also this won't work when the PC auto locks after it's been idle.

Learner89
10-21-2013, 10:47 PM
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.

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 = "htt...."

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





Thanks for the above suggestion. I copied the entire above code (with some modifications regarding URL) in the Class module and then I called the above function getData from a standard Macro. But the error appeared saying "Sub or Function not defined".

Please help. Thanks,.