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

查看:1482
本文介绍了如何在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

我也尝试过使用 removeattribute setattribute 弹出消息消失了,但是没有上传文件,因为我需要在确认消息框中按确定,单击保存按钮后会出现文件上传。

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函数使用下面的脚本,但它也显示了con公司弹出消息框:

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


推荐答案

你应该可以覆盖 ConfirmSave 函数只返回true:

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天全站免登陆