如何在VBA中调用JavaScript函数?

How to call JavaScript function in VBA?

我需要绕过 IE 确认 'OK'/'Cancel' 弹出消息。我的 VBA 脚本中 运行 一个 JavaScript 函数有问题。我的 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;
}

在手动过程中,当我点击保存按钮时,页面会弹出一个确认消息框,我的宏会在弹出窗口出现时停止运行,除非它被点击。

这是我试过的代码,点击保存按钮,

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

我也尝试使用 removeattributesetattribute 弹出消息消失了,但它没有上传文件,因为我需要在确认消息中按 'OK'单击保存按钮开始文件上传时将出现的框。

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

我使用以下脚本尝试了 运行 JavaScript 函数,但它也显示了确认弹出消息框:

Call HTMLDoc.parentWindow.execScript("submitbutton_click()")

所以我现在已经读了你的问题几次,我认为要实现你想做的事情,你将不得不完全改变你解决问题的方法。您需要阅读 Javascript 并发性、Javascript Web Worker 和 Javascript 事件循环。

只需将这些术语放入 Google 中,您就会发现很多很好的资源来了解这一点。

默认情况下 Javascript 是一种单线程语言,它会在等待事件完成其活动时停止。根据我阅读您的问题的方式,您似乎在寻找一种让您 Javascript 在显示用户提示时继续执行操作的方法。

虽然这不是背书,但我会抛出 this one link 让您入门。

您应该能够用 returns true:

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

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

甚至

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

运行 在点击按钮之前。

在 IE11 中测试并工作