如何在 VBA 中调用 JavaScript 函数? [英] How to call JavaScript function in VBA?

查看:57
本文介绍了如何在 VBA 中调用 JavaScript 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绕过 IE 确认确定"/取消"弹出消息.我在 VBA 脚本中运行 JavaScript 函数时遇到问题.我的 JavaScript:

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

我还尝试使用 removeattributesetattribute ,它们的弹出消息消失了,但它没有上传文件,因为我需要按确定"单击保存按钮开始上传文件时将出现确认消息框.

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

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

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()")

推荐答案

你应该能够用一个只返回 true 的函数覆盖 ConfirmSave 函数:

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

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

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

甚至

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

在点击按钮之前运行它.

Run that before clicking the button.

在 IE11 中测试和工作

Tested and works in IE11

这篇关于如何在 VBA 中调用 JavaScript 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆