2015-07-20 66 views
5

我相信我需要调用JavaScript方法而不是试图启动按钮。我曾尝试下面的例子,我想我的问题是,我不知道我应该使用哪个部分代码.. Click button or execute JavaScript function with VBAhttp://www.vbaexpress.com/forum/showthread.php?9690-Solved-call-a-javascript-functionhttp://www.vbforums.com/showthread.php?650771-RESOLVED-How-to-Call-a-JavaScript-function-From-Visual-Basic-6-WITH-wanted-parameters如何从vba中查找和调​​用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:"PRINT",IMPORT:"IMPORT",EXPORT:"EXPORT",ge t:function(c,b) 
{var d=[]; 
if(this._shouldHaveRowAction(this.PRINT,c)) 
{ 
if(b.getPlugin("printplugin")) 
{ 
d.push({text:"Print...",handler:b.getPlugin("printplugin").print,cls:"page- tools",scope:b.getPlugin("printplugin"),addInteractionClass:true})}} 
if(this._shouldHaveRowAction(this.IMPORT,c)) 
{ 
if(b.getPlugin("printplugin")) 
{ 
d.push({text:"Import User Stories...",handler:a.emptyFn})}} 
if(this._shouldHaveRowAction(this.EXPORT,c)) 
{ 
    if(b.getPlugin("printplugin")) 
        { 
           d.push({text:"Export as CSV",handler:a.emptyFn})}}                return d} 

接下来的这个样品是来自yui.js ...

(function() 
{ 
RALLY.ui.menu.AvailablePageTools=Ext.extend(Object, {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:'pdfexport'}; 

我试图在我的VBA脚本这些选项..

Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll) 
Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library 
Dim htmlInput As MSHTML.HTMLInputElement 
Dim htmlColl As MSHTML.IHTMLElementCollection 
Dim CurrentWindow As HTMLWindowProxy 




'Set objIE = Nothing 
Set objIE = New SHDocVw.InternetExplorer 

ThisWorkbook.Worksheets("Sheet1").Activate 


With objIE 

    .navigate "website" 
    .Visible = 1 
    Do While .readyState <> 4: DoEvents: Loop 
     Application.Wait (Now + TimeValue("0:00:02")) 

    Set htmlDoc = .document 

    objIE.document.all.Item 
     'Set CurrentWindow = objIE.htmlDoc.parentWindow 
     'no error no export 
     'Call objIE.document.parentWindow.execScript("_exportHandler:Function()", "JavaScript") 

     'no error no export 
     'Call objIE.document.parentWindow.execScript("_getExportItems:Function()", "JavaScript") 

     'RUN TIME ERROR could not complete the operation due to error 80020101 (error while evaluating js) 
     'Call objIE.document.parentWindow.execScript("_a.define()", "JavaScript") 

     'run time error the object invoked has disconnected from its clients, 
     'RUN TIME ERROR could not complete the operation due to error 80020101 (error while evaluating js) 
     'Call objIE.document.parentWindow.execScript("_b.push()", "JavaScript") 

     'run time error the object invoked has disconnected from its clients, 
     'RUN TIME ERROR could not complete the operation due to error 80020101 (error while evaluating js) 
     'Call objIE.document.parentWindow.execScript("handler:function(){window.location=Rally.ui.grid.GridExport.buildCsvExportUrl(this.gridboard.getGridOrBoard())}", "JavaScript") 

     'Call objIE.document.parentWindow.execScript("b.push()", "JavaScript") 

     'Call objIE.document.parentWindow.execScript("c.push()", "JavaScript") 

     'RUN TIME ERROR could not complete the operation due to error 80020101 (error while evaluating js) 
     'Call objIE.document.parentWindow.execScript("this.CSV_Export()", "JavaScript") 

     'error the remote server machine does not exist or is unavailable 
     'Call objIE.document.parentWindow.execScript("constructor:function()", "JavaScript") 

     'Call objIE.document.parentWindow.execScript("constructor:function()", "JavaScript") 

     'object 
     Call CurrentWindow.execScript("d.push({text:CHR(34)Export as CSV CHR(34),handler:a.emptyFn})") 

     'ofile.SaveAs scrapeRally:=ofile.Name 
     'ofile.Close savechanges:=False 

     'objIE.Quit 
     Set objIE = Nothing 

End With 

End Sub 

我想从VBA运行此所以我不能使用VAR ...

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:()函数,但我得到运行时错误无法完成,由于操作错误80020101(错误,而评估JS)

_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})} 
+0

'''不再支持execScript'''测试。从Internet Explorer 11开始,使用[eval](https://msdn.microsoft.com/en-us/library/ms536420%28v=vs.85%29.aspx)。 – dee

+0

嗨迪我跟着你的链接,并试图这两件事..我试图从VBA运行,所以我不能使用var ...'Dim getFunction =“get:function(_shouldhaverowaction(this.export,C)),( Dim exportNow'eval(“exportNow = new”+ getFunction +“;”)**我也尝试过**调用objIE.HTMLDocument.eval() “get:function(_shouldhaverowaction(this.export,C)),(b.getPlugin(chr(34)printplugin chr(34)))”,“JavaScript”)I GOT THE OBJECT DOESNT SUPPORT THIS PROPERTY OR METHOD ERROR .. – Kdunc2015

回答

1

它使用window对象的execScript方法这里简单的例子。它调用一个名为myFuncmyFuncWithParams的全局函数。 代码用IE 11.

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] })" 

    objIE.Quit 
    Set objIE = Nothing 
End Sub 

evalExample.html

<!-- 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); 
     } 
    </script> 
</head> 
<body> 
    <div>eval test</div> 
</body> 
</html> 
+0

嗨迪谢谢你的帮助!当然,我很抱歉,请原谅愚蠢的问题......对于第二个代码片段,是否应该表示我指的是我的网站源代码?或者我应该在我的vba脚本中的某个地方加入? – Kdunc2015

+0

迪网站我想代码vba反对有几个js文件。我是否应该特意引用其中的一个来调用我需要的功能?谢谢! – Kdunc2015

+0

这仅仅是一个例子(非常基础的),就是如何用''''execScript'''方法调用java-script函数。在你的情况下,你必须找到你需要执行的java脚本函数的名称,并使用这个名称来代替'myFunc()'。 – dee