KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jinterop > dcom > test > MSWord


1 package org.jinterop.dcom.test;
2
3
4
5 import java.net.UnknownHostException JavaDoc;
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 MSWord {
18
19     private JIComServer comStub = null;
20     private IJIDispatch dispatch = null;
21     private IJIComObject unknown = null;
22     
23     public MSWord(String JavaDoc address, String JavaDoc[] args) throws JIException, UnknownHostException JavaDoc
24     {
25         JISession session = JISession.createSession(args[1],args[2],args[3]);
26         comStub = new JIComServer(JIProgId.valueOf(session,"Word.Application"),address,session);
27     }
28     
29     public void startWord() throws JIException
30     {
31         unknown = comStub.createInstance();
32         dispatch = (IJIDispatch)ComFactory.createCOMInstance(ComFactory.IID_IDispatch,unknown);
33     }
34     
35     public void showWord() throws JIException
36     {
37         int dispId = dispatch.getIDsOfNames("Visible");
38         JIVariant variant = new JIVariant(Boolean.TRUE);
39         dispatch.put(dispId,variant);
40     }
41     
42     public void performOp() throws JIException, InterruptedException JavaDoc
43     {
44         System.out.println(((JIVariant)dispatch.get("Version")).getObjectAsString().getString());
45         System.out.println(((JIVariant)dispatch.get("Path")).getObjectAsString().getString());
46         JIVariant variant = dispatch.get("Documents");
47         //JIInterfacePointer ptr = variant.getObjectAsInterfacePointer();
48
//IJIDispatch documents = (IJIDispatch)ComFactory.createCOMInstance(unknown,ptr);
49
IJIDispatch documents = (IJIDispatch)variant.getObjectAsComObject(unknown);
50         JIString filePath = new JIString("c:/temp/test.doc");
51         JIVariant variant2[] = documents.callMethodA("open",new Object JavaDoc[]{filePath.VariantByRef,JIVariant.OPTIONAL_PARAM
52                 ,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,
53                 JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,
54                 JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,
55                 JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,
56                 JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM});
57         //IJIDispatch document = (IJIDispatch)ComFactory.createCOMInstance(unknown,variant2[0].getObjectAsInterfacePointer());
58
IJIDispatch document = (IJIDispatch)variant2[0].getObjectAsComObject(unknown);
59         variant = document.get("Content");
60         //IJIDispatch range = (IJIDispatch)ComFactory.createCOMInstance(unknown,variant.getObjectAsInterfacePointer());
61
IJIDispatch range = (IJIDispatch)variant.getObjectAsComObject(unknown);
62         
63         variant = range.get("Find");
64         IJIDispatch find = (IJIDispatch)variant.getObjectAsComObject(unknown);
65         
66         Thread.sleep(2000);
67         JIString findString = new JIString("ow");
68         JIString replaceString = new JIString("igh");
69         find.callMethodA("Execute",new Object JavaDoc[]{findString.VariantByRef,JIVariant.OPTIONAL_PARAM
70                 ,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,
71                 JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,
72                 JIVariant.OPTIONAL_PARAM,replaceString.VariantByRef,JIVariant.OPTIONAL_PARAM,
73                 JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM,
74                 JIVariant.OPTIONAL_PARAM});
75         Thread.sleep(5000);
76         
77         dispatch.callMethod("Quit", new Object JavaDoc[]{new JIVariant(-1,true),JIVariant.OPTIONAL_PARAM,JIVariant.OPTIONAL_PARAM});
78         JISession.destroySession(dispatch.getAssociatedSession());
79     }
80     
81     public static void main(String JavaDoc[] args) {
82
83         try {
84                 if (args.length < 4)
85                 {
86                     System.out.println("Please provide address domain username password");
87                     return;
88                 }
89                 MSWord test = new MSWord(args[0],args);
90                 test.startWord();
91                 test.showWord();
92                 test.performOp();
93             } catch (Exception JavaDoc e) {
94                 // TODO Auto-generated catch block
95
e.printStackTrace();
96             }
97     }
98     
99     
100     
101     
102     
103 }
104
Popular Tags