java 如何从java运行vbscript函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11465400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:12:54  来源:igfitidea点击:

How to run vbscript function from java?

javavbscriptpowerpoint

提问by yamini harinathan

From java code i am able to run the vbscript by using this code

从 java 代码,我可以使用此代码运行 vbscript

Runtime.getRuntime().exec("wscript C:\ppt\test1.vbs ");

But want to know how to call the method of vbscript from java..for example in test1.vbs

但是想知道如何从java调用vbscript的方法..例如在test1.vbs

Set objPPT = CreateObject("PowerPoint.Application")
    objPPT.Visible = True
    Set objPresentation = objPPT.Presentations.Open("C:\ppt\Labo.ppt")
    Set objSlideShow = objPresentation.SlideShowSettings.Run.View
    sub ssn1()
      objPPT.Run "C:\ppt\Labo.ppt!.SSN"
    End sub

how to call only ssn1() method from java.Otherwise can we run the macro of a power point from java code..kindly help!!

如何从java中只调用ssn1()方法。否则我们可以从java代码运行power point的宏..请帮忙!!

回答by Frederik.L

This should make you happy :) Go to the WScript section : http://technet.microsoft.com/library/ee156618.aspx

这应该会让你高兴:) 转到 WScript 部分:http: //technet.microsoft.com/library/ee156618.aspx

Here's my idea... in your vbscript file, make your script listen to a command line parameter that would specify which method to call. Then, in Java, you could only have to use this parameter whenever you want to call a specific method in the file.

这是我的想法...在您的 vbscript 文件中,让您的脚本侦听将指定调用哪个方法的命令行参数。然后,在 Java 中,您只能在要调用文件中的特定方法时使用此参数。

Otherwise, if you want to access powerpoint in java, you will need to access its API like you did in vbscript, which is possible if vbscript can do it but the approach / syntax may change.

否则,如果您想在 Java 中访问 powerpoint,您将需要像在 vbscript 中一样访问它的 API,如果 vbscript 可以做到这一点,这是可能的,但方法/语法可能会改变。

回答by Stephen Rindsberg

The PowerPoint application object's .Run method lets you call any public subroutine or function in any open presentation or loaded add-in

PowerPoint 应用程序对象的 .Run 方法允许您在任何打开的演示文稿或加载的加载项中调用任何公共子例程或函数

回答by Erik Eidt

This post answers the OP's question:

这篇文章回答了 OP 的问题:

Otherwise can we run the macro of a power point from java code..kindly help!!

否则我们可以从java代码运行power point的宏..请帮忙!!

(but does not address the original vbscript question)

(但没有解决原来的 vbscript 问题)

There's the JACOB library, which stands for Java COM Bridge, you can find here: http://sourceforge.net/projects/jacob-project/?source=directory

有 JACOB 库,它代表 Java COM Bridge,您可以在这里找到:http: //sourceforge.net/projects/jacob-project/?source=directory

With it you can invoke Excel, Word, Outlook, PowerPoint application object model methods.

使用它您可以调用 Excel、Word、Outlook、PowerPoint 应用程序对象模型方法。

I've tried this with Excel but not PowerPoint. (This is just some sample code, one might want to make it more object oriented.)

我用 Excel 试过这个,但没有用 PowerPoint 试过。(这只是一些示例代码,人们可能想让它更面向对象。)

public class Excel {
    private static ActiveXComponent xl = null;

    public static Init() {
        try {
            ComThread.InitSTA();
            xl = ActiveXComponent.connectToActiveInstance("Excel.Application.14"); 
            // 14 is Office 2010, if you don't know what version you can do "Excel.Application"
            if (xl==null) {
                // code to launch Excel if not running:
                xl = new ActiveXComponent("Excel.Application");
                Dispatch.put(xl, "Visible", Constants.kTrue);
            }
        }
        catch (Exception e) {
            ComThread.Release();            
        }
    }

    public static String Run(String vbName) {
        // Variant v = Dispatch.call(xl, "Run", vbName);    // using string name lookup
        Variant v = Dispatch.call(xl, 0x103, vbName);       // using COM offset
        // return Dispatch.get(this, "Name").getString();
        return v.getString();       
    }

    public static Variant Run1p(String vbName, Object param) {
        // Variant v = Dispatch.call(xl, "Run", vbName, param);
        return Dispatch.call(xl, 0x103, vbName, param);
        // return Dispatch.get(this, "Name").getString();
    }

    public static Worksheet GetActiveWorksheet () {
        // Dispatch d = xl.getProperty("ActiveSheet").toDispatch();
        Dispatch d = Dispatch.get(xl, 0x133).toDispatch ();
        return d; // you may want to put a wrapper around this...
    }
}

Notes:

笔记:

For Excel, at least, to get Run to invoke a VBA macro/subroutine several things have to be true:

至少对于 Excel,要让 Run 调用 VBA 宏/子例程,必须满足以下几点:

  1. The Excel workbook containing the macro must be "Active" (i.e. must be the ActiveWorkbook) otherwise Run will not find the VBA subroutine. (However the workbook does not have to be screen visible!! This means you can call a VBA Macro that is in an add-in!).
  2. You can then pass the name of the macro using the following syntax as a string literal:
  1. 包含宏的 Excel 工作簿必须是“活动的”(即必须是 ActiveWorkbook),否则 Run 将找不到 VBA 子例程。(但是,工作簿不必在屏幕上可见!这意味着您可以调用加载项中的 VBA 宏!)。
  2. 然后,您可以使用以下语法作为字符串文字传递宏的名称:

VBAProjectName.VBAModuleName.SubroutineName

VBAProjectName.VBAModuleName.SubroutineName

For COM object invocations, you can use the name lookup version or the id number version. The id numbers come from the published COM interfaces (which you can find in C++ header files, or possibly have JACOB look them up for you).

对于 COM 对象调用,您可以使用名称查找版本或 ID 编号版本。id 编号来自已发布的 COM 接口(您可以在 C++ 头文件中找到,或者可能让 JACOB 为您查找)。

If you successfully did the connection to Excel, be sure to call ComThread.Release() when you're done. Put it in some appropriately surrounding finally. If the process of your Java code terminates without calling it, the COM reference count on Excel will be wrong, and the Excel process will never terminate, even after you exit the Excel application. Once that happens, needless to say, Excel starts to behave screwy then (when you try to use it next, it runs but will fail to load any plug-ins/add-ons). If that happens (as it can during debugging esp. if you are bypassing finally's for better debugging) you have to use the task manager to kill the Excel process.

如果您成功连接到 Excel,请确保在完成后调用 ComThread.Release()。最后把它放在一些适当的环境中。如果 Java 代码的进程没有调用就终止了,Excel 上的 COM 引用计数将是错误的,并且 Excel 进程将永远不会终止,即使在您退出 Excel 应用程序之后也是如此。一旦发生这种情况,不用说,Excel 就会开始表现得很糟糕(当您下次尝试使用它时,它会运行但无法加载任何插件/附加组件)。如果发生这种情况(尤其是在调试期间可能会发生这种情况,如果您绕过 finally 以进行更好的调试),您必须使用任务管理器来终止 Excel 进程。

回答by geby

I'm not so much into the visual basic script side, but if you can expose your visual basic script as a COM object, the you can access the methods of it from java by usage of frameworks such as for example com4j:

我对visual basic脚本方面不太了解,但是如果您可以将visual basic脚本公开为COM对象,则可以通过使用框架(例如com4j)从java访问它的方法:

http://com4j.java.net/

http://com4j.java.net/