Consulting

Results 1 to 6 of 6

Thread: Excel VBA controlling IE confirmation popup box

  1. #1
    VBAX Newbie
    Joined
    Jun 2011
    Posts
    1
    Location

    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?

  2. #2

    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]

  3. #3
    VBAX Regular
    Joined
    Dec 2012
    Posts
    7
    Location
    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.
    [vba]
    <INPUT
    style="WIDTH: 100px" id= StatusBar_btnDelete class=MyButton
    onclick="DeleteBtnClicked(); return false;" value=Delete type=submit name=
    StatusBar$btnDelete>






    [/vba]




    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.
    [vba]

    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
    [/vba]


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

  4. #4

  5. #5
    VBAX Regular
    Joined
    Dec 2012
    Posts
    7
    Location
    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.

  6. #6
    Quote Originally Posted by Shred Dude View Post
    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 = "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


    [/VBA]

    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,.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •