3

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

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

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!!

4 Answers 4

1

This should make you happy :) Go to the WScript section : 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.

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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:

http://com4j.java.net/

Comments

0

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

Comments

0

This post answers the OP's question:

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

(but does not address the original vbscript question)

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

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

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

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:

  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:

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).

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.