1

I need to by pass an IE confirm 'OK'/'Cancel' pop-up message. I have a problem running a JavaScript function in my VBA script. My JavaScript:

 function ConfirmSave()
{
var Ok = confirm('Are you sure all Documents and Information are attached and correct before saving?');

if(Ok)
return true;
else
return false;
}


function submitbutton_click() {
    document.getElementById('FileAttachement2_hdnButtonFlag').value = "SAVE";
    var submitbutton = document.getElementById('cmdDownSave');
    var uploadobj=document.getElementById('FileAttachement2_Uploader1');
    if(!window.filesuploaded)
    {
       if (!ConfirmSave()) return false;
        if(uploadobj.getqueuecount()>0)
        {

            uploadobj.startupload();
        }
        else
        {
            //var uploadedcount=parseInt(submitbutton.getAttribute("itemcount"))||0;
            //if(uploadedcount>0)
            //{
                return true;
            //}
            //alert("Please browse files for upload");
        }
        return false;
    }
    window.filesuploaded=false;
    return true;
}

In manual process, when I click the save button, the page will pop-up a confirm message box, and my macro will stop running when the pop-up appears unless it has been clicked.

Here is the code I have tried, to click the save button,

Set ElementNameV = HTMLDoc.getElementsByName("cmdsave")
ElementNameV(0).click

I also tried using removeattribute and setattribute with which the pop-up message disappeared but it doesn't upload the file because I need to press the 'OK' in confirm message box that will appear upon clicking the save button to start the file uploading.

ElementNameV(0).removeAttribute ("onclick")
ElementNameV(0).setAttribute "onclick", "return true"
ElementNameV(0).click

I tried running the JavaScript function using below script but it also shows the confirm pop-up message box:

Call HTMLDoc.parentWindow.execScript("submitbutton_click()")
pnuts
  • 58,317
  • 11
  • 87
  • 139
Sandy
  • 23
  • 1
  • 1
  • 8
  • May I ask why you aren't using the native VBA/VBScript methods? – Bryan C. May 14 '15 at 02:21
  • @BryanC. what do u mean by native VBA methods? can you share some suggestions on how do I able to control the pop-up confirm message when it appears? because my codes stop running when I click the save button and the pop-up appears – Sandy May 14 '15 at 02:25
  • Take a look at this page... this is a VBScript OK/Cancel message box: http://stackoverflow.com/questions/3062401/vb-script-how-to-create-msgbox-with-two-buttons see selected answer at the top. – Bryan C. May 14 '15 at 02:27
  • This may or may not be useful depending on what you're doing. – Bryan C. May 14 '15 at 02:35
  • that's not what I am looking for, I do not need to create a pop-up message. what I need is to handle the pop-up message that will appear when my I click the save button from a web page. – Sandy May 14 '15 at 02:36
  • Hmm... Then I apologize. I guess I'm not really understanding the question. This part here "In manual process, when I click the save button, the page will pop-up a confirm message box, and my macro will stop running when the pop-up appears unless it has been clicked." That's by design. You're trying to avoid that? – Bryan C. May 14 '15 at 02:38
  • I tried to avoid the pop-up using the removeattribute and setattribute. the pop-up is gone but the problem is the file isn't uploading that is because I need to click the 'OK' in the pop-up message to start uploading the file. I don't think avoiding the pop-up confirm message will help. I think I need to control it when it appears. but the problem is my macro stop working when it appears. I think I need to use the Events but Im afraid I don't know how. – Sandy May 14 '15 at 02:56
  • Ok, well, hopefully someone else will chime in. – Bryan C. May 14 '15 at 02:58

2 Answers2

4

You should be able to overwrite the ConfirmSave function with one which simply returns true:

HTMLDoc.parentWindow.execScript "window.ConfirmSave = function(){return true;};"

or

HTMLDoc.parentWindow.execScript "window.confirm = function(){return true;};"

or even

HTMLDoc.parentWindow.eval "window.confirm = function(){return true;};"

Run that before clicking the button.

Tested and works in IE11

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • btw, how can I Run this using IE11? because I think this code works only in old version of IE? – Sandy May 14 '15 at 07:29
  • It works with my friend using IE9 but it doesn't work with me using IE11. do you have any idea? – Sandy May 14 '15 at 07:31
  • meaning, it works with IE version 9. but in IE ver 11, nothing happens when the code runs. – Sandy May 18 '15 at 05:00
  • See my edit above. Both suggestions tested and working in IE11 – Tim Williams May 18 '15 at 06:37
  • It doesnt worked for me, I don't know why, but I think it has something to do with this [execScript is no longer supported. Starting with Internet Explorer 11](https://msdn.microsoft.com/en-us/library/ms536420(v=vs.85).aspx) – Sandy May 19 '15 at 13:50
  • However it works for me in IE11. Are you using the 64-bit version of IE? Maybe try it with the 32-bit version. – Tim Williams May 19 '15 at 16:37
  • I am only using 32-bit version. however, do you have an idea how to translate the code you provided?instead of using execScript use [Eva Function](https://msdn.microsoft.com/en-us/library/12k71sw7(v=vs.85).aspx) – Sandy May 20 '15 at 04:07
  • Don't have a PC here but have you tried using Eval ? – Tim Williams May 20 '15 at 07:09
  • The problem is I dont know how to use Eval FUnction. Can you help me sort it out pls? thank you! – Sandy May 20 '15 at 11:38
  • Replace ExecScript with Eval. – Tim Williams May 20 '15 at 15:08
  • HTMLDoc.parentWindow.Eval "window.ConfirmSave = function(){return true;};"
    HTMLDoc.parentWindow.Eval "window.Confirm = function(){return true;};"
    HTMLDoc.parentWindow.Eval "window.confirm = function(){return true;};"
    HTMLDoc.parentWindow.execScript "window.confirm = function(){return true;};"
    HTMLDoc.parentWindow.execScript "window.Confirm = function(){return true;};"
    HTMLDoc.parentWindow.execScript "window.ConfirmSave = function(){return true;};"
    Sleep 500
    ElementNameV(ElementItem).click
    – Sandy May 22 '15 at 05:39
  • I tried all of the code above and no luck.
    how about using the
    Set and Remove Attribute?
    would it be helpful?
    ElementNameV(ElementItem).removeAttribute ("onclick")
    ElementNameV(ElementItem).setAttribute "onclick", "return SOMETHING?"
    – Sandy May 22 '15 at 05:41
  • All three of the lines in my answer work for me on IE11, so there's not much I can suggest here. I have no idea why it wouldn't work for you. If you can post an actual URL then I can confirm on the page in question, otherwise I'm out of ideas. – Tim Williams May 22 '15 at 05:43
  • Hi Tim, Do you have an email?so I can send a copy to you?you cannot view the URL because it was covered in our intranet. I dont even have an idea how can I attach file here :( – Sandy May 22 '15 at 07:26
  • I will save the html page of the SIte I am trying to automate so you can check it. just share me your email. I am not able to upload it to any file sharing site because it is also blocked in our network. – Sandy May 22 '15 at 07:31
  • how about I force it to run the javascript function uploadobj.startupload(); ? can you help me how to execute it? – Sandy May 28 '15 at 13:50
  • Just wrap that line in a call to Eval like the second example in my answer above. – Tim Williams May 28 '15 at 14:50
-1

So I've read your question a few times now and I think that to achieve what you want to do you are going to have to completely change your approach to the problem. You need to read up on Javascript Concurency, Javascript Web Workers, and the Javascript Event Loop.

Just throw these terms into Google and you'll find lots of great resources to learn about this.

By default Javascript is a single threaded language and it halts while waiting for events to complete their activities. What you seem to be looking for based on how I'm reading your question is a way for your Javascript to keep performing actions while a user prompt is being displayed.

While this is not an endorsement, I will throw out this one link to get you started.

Bryan C.
  • 143
  • 1
  • 8
  • my automation is written in VBA script. not javascript, I just need to interact with javascript to run my macro. but then, I will take a look with those Javascript Concurency, Javascript Web Workers, and the Javascript Event Loop – Sandy May 14 '15 at 02:58
  • You know what... maybe... take a look at this other question and see what you think: http://stackoverflow.com/questions/10008556/upload-a-file-via-a-form-using-asp and look at that top answer. Maybe that's what you're looking for. – Bryan C. May 14 '15 at 03:04