SAMPLE: How to Automate Word 8.0 from Java

Last reviewed: January 29, 1998
Article ID: Q169802
The information in this article applies to:
  • Microsoft Visual J++, versions 1.0, 1.1
  • SDK for Java 2.0
  • Microsoft Win32 Virtual Machine for Java

SUMMARY

The Jword8.exe sample illustrates how to automate Word 97 from a Java application. This sample shows how to invoke Word 97, make it visible, and open up an existing Word Document. It also PrintPreviews the document and Prints it. It uses the Open, PrintPreview, and PrintOut methods of Word 97.

The following file is available for download from the Microsoft Software Library:

 ~ Jword8.exe (size: 29766 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from Online Services


MORE INFORMATION

Overview of Jword8.exe

This sample illustrates how to call a COM object like Word 97 from Java. It shows how you can invoke Word 97, make it visible, and open an existing Word 97 document. It also PrintPreviews and Prints the document. It makes use of the methods like Open, PrintPreview and PrintOut and properties like putVisible of the Word 97 Type Library.

How to Use Jword8.exe

  1. Using Visual J++, create a default Java Applet Wizard Applet or Application or both.

  2. Run JavaTLB on Microsoft Word 8.0 Object, this creates a Java interface for the COM Object. The Java Type Library Wizard is a tool that reads type libraries from .TLB, .OLB, .OCX, .DLL, and .EXE files and creates Java class files with information about the COM interfaces and methods. You need these class files for both compiling and running a Java project using COM components. If you are using JACTIVEX, then you will need to build this sample using the latest JVC.EXE that ships with the SDK2.0 for Java.

  3. After you have created Java descriptions of COM components, the output window shows two useful types of information, the import statement and the SUMMARY.TXT notice. It displays the following Java import statement:

          import msword8.*;
    

    Copy this line of text and insert it into the beginning of any Java source file that uses the COM component. This has been done for you in this sample (see line 9 in JWord8.java).

    In the summary.txt line you will note that there are no methods listed for any of the classes. Recall that when you call COM from Java, you use the class file to create the COM component, but you must use the interface file to access the COM component. Double-click the Summary.txt line to view that file in Developer Studio.

  4. Refer to the public method called "CallWord" in the JWord8.java from the sample. This code shows how to use the methods and properties of the Word 97 Object.

  5. In Developer Studio, build the applet.

  6. In Developer Studio, run the applet. If this is the first time you have run the applet, Developer Studio will prompt you for the name of the class. Type: JWord8

Security

COM components can get access to any system resources. As such, they are both very powerful and potentially very dangerous. In the Java Virtual Machine (VM) in Microsoft Internet Explorer 3.x only trusted class files can use COM components. Class files from digitally signed CAB files are trusted.

If the HTML file is run from Microsoft Developer Studio, the class files are also trusted. This can be very helpful during applet development. However, to deliver your applet to other users, you must put it in a signed CAB file.

When it is run as an application, the class file must already be on your machine. When it is run as an applet, the class file may be downloaded to your machine. There is a potential security threat running downloaded software. To run a Java applet that uses COM components, the applet code must be trusted. For more information on trusted class files, see the CabAndSign sample.

Project Files in Jword8.exe

  1. JWord8.java: the Java source code for the sample.

  2. JWord8Frame.java: the Java source code for the sample as a stand-alone

        application.
    

  3. JWord8.dsw: the project workspace file.

  4. JWord8.dsp: the project file.

  5. JWord8.html: the HTML for the sample applet.

REFERENCES

You can download the SDK for Java 2.0 at http://www.microsoft.com/java/.

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following page on the Microsoft Technical Support site:

   http://support.microsoft.com/support/visualj/
   http://support.microsoft.com/support/java/

Keywords          : kbsample kbtool JCOM JVM WizardIss
Technology        : kbInetDev
Version           : 1.0 1.1
Platform          : WINDOWS
Solution Type     : kbfile


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 29, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.