SAMPLE: Jword8.exe Demonstrates Automating Word 8.0 from Java

ID: Q169802


The information in this article applies to:


SUMMARY

The Jword8.exe sample illustrates how to automate Microsoft Word 97 version 8.0 (MS Word) 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 MS Word 97.


MORE INFORMATION

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

Jword8.exe

After downloading the Jword8.exe file, extract the files into a new directory and ensure the files are named as seen in the "Project Files in Jword8.exe" section below.

Overview of Jword8.exe

This sample illustrates how to call a COM object like Word 97 from Java. It shows how you can invoke MS Word, make it visible, and open an existing MS Word document. It also PrintPreviews and Prints the document. It makes use of the methods like Open, PrintPreview and PrintOut and properties like putVisible (or setVisible) of the MS Word 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 instead of JavaTLB, then you will need to build this sample using the latest JVC.exe that ships with the SDK for Java 2.0 and later. The file that contains the Word 8.0 interface information is msword8.olb.


  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 MS Word object. If you are using a machine with Office 97, you will need to change one line in the method CallWord as seen here:
    
    //   appWord.putVisible(true);
    appWord.setVisible(true); 


  5. In Developer Studio, build the applet.


  6. Using MS Word, create a new document. Add some text to it. Save the file as Hello.doc in your c:\temp directory. Exit MS Word.


  7. 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. Alternatively, you can modify the APPLET tag in the HTML file to point to JWord8.class.


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 and higher 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 Knowledge Base Article, HOWTO: Make Your Java Code Trusted in Internet Explorer.

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.0x at the following Web site:

http://www.microsoft.com/java/
For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, please see the following pages on the Microsoft Technical Support site:
http://support.microsoft.com/support/visualj/

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

Additional query words:


Keywords          : kbfile kbsample kbtool kbwizard kbMFC kbSDKJava300 kbVC kbSDKJava310 JCOM JVM kbSDKJava320 
Version           : 1.0 1.1 2.0 2.01 2.02 3.0 3.1 3.2
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: July 14, 1999