1 package org.jinterop.dcom.test; 2 3 4 5 import java.net.UnknownHostException ; 6 7 import org.jinterop.dcom.common.JIException; 8 import org.jinterop.dcom.core.IJIComObject; 9 import org.jinterop.dcom.core.JIComServer; 10 import org.jinterop.dcom.core.JIProgId; 11 import org.jinterop.dcom.core.JISession; 12 import org.jinterop.dcom.core.JIString; 13 import org.jinterop.dcom.core.JIVariant; 14 import org.jinterop.dcom.win32.ComFactory; 15 import org.jinterop.dcom.win32.IJIDispatch; 16 17 public class MSPowerPoint { 18 19 private JIComServer comStub = null; 20 private IJIDispatch dispatch = null; 21 private IJIComObject unknown = null; 22 23 public MSPowerPoint(String address, String [] args) throws JIException, UnknownHostException 24 { 25 JISession session = JISession.createSession(args[1],args[2],args[3]); 26 comStub = new JIComServer(JIProgId.valueOf(session,"PowerPoint.Application"),address,session); 27 } 28 29 public void startPowerPoint() throws JIException 30 { 31 unknown = comStub.createInstance(); 32 dispatch = (IJIDispatch)ComFactory.createCOMInstance(ComFactory.IID_IDispatch,unknown); 33 } 34 35 public void showPowerPoint() throws JIException 36 { 37 int dispId = dispatch.getIDsOfNames("Visible"); 38 JIVariant variant = new JIVariant(-1); 39 dispatch.put(dispId,variant); 40 } 41 42 public void performOp() throws JIException, InterruptedException 43 { 44 IJIDispatch presentations = (IJIDispatch)dispatch.get("Presentations").getObjectAsComObject(unknown); 48 49 for (int i = 0; i < 2; i++) 50 { 51 JIVariant results[] = presentations.callMethodA("Add",new Object []{JIVariant.OPTIONAL_PARAM}); 52 IJIDispatch presentation = (IJIDispatch)results[0].getObjectAsComObject(unknown); 56 IJIDispatch slides = (IJIDispatch)presentation.get("Slides").getObjectAsComObject(unknown); 60 61 results = slides.callMethodA("Add", new Object []{new Integer (1),new Integer (1)}); 62 IJIDispatch slide = (IJIDispatch)results[0].getObjectAsComObject(unknown); 65 66 IJIDispatch shapes = (IJIDispatch)slide.get("Shapes").getObjectAsComObject(unknown); 69 70 IJIDispatch shape = (IJIDispatch)shapes.get("Title").getObjectAsComObject(unknown); 73 74 IJIDispatch textframe = (IJIDispatch)shape.get("TextFrame").getObjectAsComObject(unknown); 77 78 IJIDispatch textrange = (IJIDispatch)textframe.get("TextRange").getObjectAsComObject(unknown); 81 82 if (i == 0) 83 { 84 textrange.put("Text",new JIString("Presentation1").Variant); 85 presentation.callMethod("SaveAs", new Object []{new JIString("C:\\temp\\presentation1.ppt").Variant,JIVariant.OPTIONAL_PARAM,new Integer (-1)}); 86 Thread.sleep(3000); 87 presentation.callMethod("Close"); 88 } 89 else 90 { 91 textrange.put("Text",new JIString("Presentation2").Variant); 92 slides.callMethod("InsertFromFile", new Object []{new JIString("C:\\temp\\presentation1.ppt"),new Integer (1), new Integer (1), new Integer (1)}); 93 presentation.callMethod("SaveAs", new Object []{new JIString("C:\\temp\\presentation2.ppt"),JIVariant.OPTIONAL_PARAM,new Integer (-1)}); 94 Thread.sleep(3000); 95 presentation.callMethod("Close"); 96 97 dispatch.callMethod("Quit"); 98 } 99 100 101 } 102 103 JISession.destroySession(dispatch.getAssociatedSession()); 104 } 105 106 public static void main(String [] args) { 107 108 try { 109 if (args.length < 4) 110 { 111 System.out.println("Please provide address domain username password"); 112 return; 113 } 114 MSPowerPoint test = new MSPowerPoint(args[0],args); 115 test.startPowerPoint(); 116 test.showPowerPoint(); 117 test.performOp(); 118 } catch (Exception e) { 119 e.printStackTrace(); 121 } 122 } 123 124 125 126 127 128 } 129 | Popular Tags |