Microsoft KB Archive/169802
Jword8.exe Demonstrates Automating Word 8.0 from Java
The information in this article applies to:
- Microsoft Visual J++, versions 1.0, 1.1
- Microsoft SDK for Java, versions 2.0, 2.01, 2.02, 3.0, 3.1, 3.2
- Microsoft virtual machine
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.
The following files are available for download from the Microsoft Download Center. Click the file names below to download the files:
For additional information about how to download Microsoft Support files, click the article number below to view the article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online Services
Microsoft used the most current virus detection software available on the date of posting to scan this file for viruses. Once posted, the file is housed on secure servers that prevent any unauthorized changes to the file.
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
- Using Visual J++, create a default Java Applet Wizard Applet or Application or both.
- 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.
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:
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.
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);
- In Developer Studio, build the applet.
- 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.
- 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.
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
- JWord8.java: the Java source code for the sample.
- JWord8Frame.java: the Java source code for the sample as a stand-alone application.
- JWord8.dsw: the project workspace file.
- JWord8.dsp: the project file.
- JWord8.html: the HTML for the sample applet.
You can download the SDK for Java 2.0x at the following Web site:
For the latest Microsoft 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 Web site:
Additional query words:
Keywords : kbfile kbsample kbtool kbwizard kbMFC kbSDKJava300 kbVC kbCodeDownload kbSDKJava310 kbSDKJava320 kbSDKJavaFAQ kbJavaVMFAQ kbJavaFAQ
Issue type : kbinfo
Last Reviewed: October 24, 2000