Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从vba中查找和调用javascript方法_Javascript_Vba - Fatal编程技术网

如何从vba中查找和调用javascript方法

如何从vba中查找和调用javascript方法,javascript,vba,Javascript,Vba,我认为我需要调用一个javascript方法,而不是尝试启动按钮。我尝试了以下示例,我认为我的问题是我不知道应该使用代码的哪些部分。。这个和 我无法发布链接,因为它需要登录才能访问。。。如果您需要更大的代码示例来理解我的问题,请告诉我 我相信我需要一个引用“导出为CSV”的函数,但我不确定调用哪个函数以及调用中包含函数的哪些部分。。。以下内容来自catalog.js function() {var a=window.Ext4 | | window.Ext;a.define(“Rally.alm.

我认为我需要调用一个javascript方法,而不是尝试启动按钮。我尝试了以下示例,我认为我的问题是我不知道应该使用代码的哪些部分。。这个和

我无法发布链接,因为它需要登录才能访问。。。如果您需要更大的代码示例来理解我的问题,请告诉我

我相信我需要一个引用“导出为CSV”的函数,但我不确定调用哪个函数以及调用中包含函数的哪些部分。。。以下内容来自catalog.js

function()
{var a=window.Ext4 | | window.Ext;a.define(“Rally.alm.ui.page.plugins.PageToolFactory”),{singleton:true,mixins:{messageable:Rally.messageable},PRINT:“打印”,IMPORT:“导入”,EXPORT:“导出”,ge t:函数(c,b)
{var d=[];
如果(此._应保存操作(此.打印,c))
{
if(b.getPlugin(“printplugin”))
{
d、 push({text:“Print…”,处理程序:b.getPlugin(“printplugin”).Print,cls:“page-tools”,作用域:b.getPlugin(“printplugin”),addInteractionClass:true}}
if(this._shouldlaverowaction(this.IMPORT,c))
{
if(b.getPlugin(“printplugin”))
{
d、 push({text:“导入用户故事…”,处理程序:a.emptyFn})}
if(this._shouldlaverowaction(this.EXPORT,c))
{
if(b.getPlugin(“printplugin”))
{
d、 push({text:“导出为CSV”,处理程序:a.emptyFn}}}返回d}
下一个示例来自yui.js

(函数()
{
RALLY.ui.menu.AvailablePagetTools=Ext.extend(对象,{constructor:function()
{
this.PRINT={text:'PRINT…',slug:'PRINT'};
this.EMAIL={text:'EMAIL…',slug:'EMAIL'};
this.SAVE={text:'SAVE…',slug:'SAVE'};
this.CSV_IMPORT={text:'IMPORT User Stories…',slug:'csvimport'};
this.CSV_IMPORT_PORTFOLIO_ITEM={text:'IMPORT PORTFOLIO Items…',slug:'csvimport'};
this.CSV_EXPORT={text:'EXPORT as CSV',slug:'csvexport'};
this.PDF_EXPORT={text:'EXPORT as PDF',slug:'pdfeexport'};
我已在vba脚本中尝试了这些选项

SHDocVw.InternetExplorer的microsoft internet控件(SHDocVw.dll)
将htmlDoc设置为MSHTML.HTMLDocument的Microsoft HTML对象库
将htmlInput设置为MSHTML.HTMLInputElement
将htmlColl变暗为MSHTML.IHTMLElementCollection
将CurrentWindow设置为HTMLWindowProxy
'设置对象=无
Set objIE=New SHDocVw.InternetExplorer
此工作簿。工作表(“表1”)。激活
与奥布杰
.浏览“网站”
.Visible=1
执行While.readyState 4:DoEvents:Loop
Application.Wait(现在+时间值(“0:00:02”))
设置htmlDoc=.document
objIE.document.all.Item
'设置CurrentWindow=objIE.htmlDoc.parentWindow
'没有错误,没有导出
'调用objIE.document.parentWindow.execScript(“\u exportHandler:Function()”,“JavaScript”)
'没有错误,没有导出
'调用objIE.document.parentWindow.execScript(“\u getExportItems:Function()”,“JavaScript”)
'由于错误80020101,运行时错误无法完成操作(评估js时出错)
'调用objIE.document.parentWindow.execScript(“\u a.define()”,“JavaScript”)
'运行时错误调用的对象已与其客户端断开连接,
'由于错误80020101,运行时错误无法完成操作(评估js时出错)
'调用objIE.document.parentWindow.execScript(“_b.push()”,“JavaScript”)
'运行时错误调用的对象已与其客户端断开连接,
'由于错误80020101,运行时错误无法完成操作(评估js时出错)
调用objIE.document.parentWindow.execScript(“处理程序:function(){window.location=Rally.ui.grid.GridExport.buildCsvExportUrl(this.gridboard.getGridOrBoard())}”,“JavaScript”)
'调用objIE.document.parentWindow.execScript(“b.push()”,“JavaScript”)
'调用objIE.document.parentWindow.execScript(“c.push()”,“JavaScript”)
'由于错误80020101,运行时错误无法完成操作(评估js时出错)
'调用objIE.document.parentWindow.execScript(“this.CSV_Export()”,“JavaScript”)
'错误远程服务器计算机不存在或不可用
'调用objIE.document.parentWindow.execScript(“构造函数:函数()”,“JavaScript”)
'调用objIE.document.parentWindow.execScript(“构造函数:函数()”,“JavaScript”)
“反对
调用CurrentWindow.execScript(“d.push({text:CHR(34)导出为CSV-CHR(34),handler:a.emptyFn})”)
'ofile.SaveAs:=ofile.Name
'ofile.Close savechanges:=False
“objIE,退出
设置对象=无
以
端接头
我试图从VBA运行这个,所以我不能使用变量

Dim getFunction = "get:function (_shouldhaverowaction(this.export,C)),(b.getPlugin(chr(34)printplugin chr(34)))" 

Dim exportNow 
eval ("exportNow = new" + getFunction + ";") 
我也试过了

Call objIE.HTMLDocument.eval("get:function (_shouldhaverowaction(this.export,C)),(b.getPlugin( chr(34) printplugin chr(34)))", "JavaScript") 
当我尝试运行该对象时,出现了“该对象不支持此属性或方法”错误

任何建议都非常好

我刚刚遇到了另一个可能是我想要的函数,但我仍然不知道应该调用它的哪一部分。我尝试只调用_getExportItems:function(),但我得到运行时错误,由于错误80020101无法完成操作(评估js时出错)


下面是一个简单的例子,它使用了
窗口
对象的
execScript
方法。它调用了名为
myFunc
myFuncWithParams
的全局函数。代码用IE 11进行了测试

选项显式
Private Const Url为String=“c:\Temp\evalExample.html”
公共子测试()
Dim objIE作为SHDocVw.InternetExplorer
将currentWindow设置为HTMLWindowProxy
Set objIE=New SHDocVw.InternetExplorer
objIE.navigate Url
objIE.Visible=1
在objIE.readyState 4时执行
多芬特
环
设置currentWindow=objIE.document.parentWindow
'这里调用了不带参数的函数。
currentWindow.execScript代码:=“myFunc()”
'这里调用带有参数的函数。
“F
 _getExportItems:function(){
 var b=[];
 if(this.enableCsvExport){
b.push({
text:"Export to CSV...",
handler:function(){
window.location=Rally.ui.grid.GridExport.buildCsvExportUrl   (this.gridboard.getGridOrBoard())},scope:this})}
Option Explicit

Private Const Url As String = "c:\Temp\evalExample.html"

Public Sub Test()
    Dim objIE As SHDocVw.InternetExplorer
    Dim currentWindow As HTMLWindowProxy

    Set objIE = New SHDocVw.InternetExplorer
    objIE.navigate Url
    objIE.Visible = 1

    Do While objIE.readyState <> 4
        DoEvents
    Loop

    Set currentWindow = objIE.document.parentWindow

    ' Here the function without parameters is called.
    currentWindow.execScript code:="myFunc()"

    ' Here the function with parameters is called.
    ' First parameter is numeric, then string, then boolean and finally object.
    ' The object has three properties, numeric, string and array.
    currentWindow.execScript code:="myFuncWithParams(123, 'Some text', true, { property1: 555, property2: 'hi there from object', property3: [111,222,333] })"

    ' And here function which receives parameters and returns value.
    ' The return value is then available through hidden property of document named 'Script'.
    ' Thanks to this answer: https://stackoverflow.com/questions/9005914/retrieve-return-value-of-a-javascript-function-in-the-webbrowser-control-in-vb6
    currentWindow.execScript code:="func = function(){return myFuncReturns(2,3);}; retVal = func();"
    Dim result As Long
    result = objIE.document.Script.retVal
    Debug.Print result ' for 2 and 3 prints 5

    objIE.Quit
    Set objIE = Nothing
 End Sub
<!-- saved from url=(0016)http://localhost -->
<html>
<head>
    <script type="text/javascript">
        function myFunc() {
            var date = new Date();
            alert("Hi from my parameter-less 'myFunc'. Day of the month: " + date.getDate());
        }

        function myFuncWithParams(a, b, c, d) {
            var number = a + 100,
                text = b + " from my func2",
                bool = !c,
                obj = d;

            var alertText = "\nNumber = '" + number + "'" +
                            "\nText = '" + text + "'" +
                            "\nBoolean = '" + bool + "'" +
                            "\nObject.property1 = '" + obj.property1 + "'" +
                            "\nObject.property2 = '" + obj.property2 + "'" +
                            "\nObject.property3.lenght = '" + obj.property3.length + "'" + 
                            "\nObject.property3[2] = '" + obj.property3[2] + "'";

            alert("Hi from my 'myFunc2' with parameters.\n" + alertText);
        }

        function myFuncReturns(a, b) {
            return a + b;
        }
    </script>
</head>
<body>
    <div>eval test</div>
</body>
</html>