Solved

Java Code for Opening A WordML file and saving it as a Word doc file

Posted on 2007-11-23
7
1,866 Views
Last Modified: 2013-11-24
I am working on a web application that produces WordML files. This is fine for users who have Word 2003 installed which supports WordML.
Unfortunately some users only have Word XP (2002) and cannot open and read the WordML documents. For those users I would like to provide the documents in the binary .doc format.
There seems to be no converter. Therefore I want to do the following.
Either in a servlet or a JSP page
1.) I want to take the WordML document that was created
2.) Open an instance of Word 2003 on ther server
3.) Load the document in this Word 2003 Instance
4.) Save the document as .doc
5.) Put the .doc in the response object and send it to my users

How could I do that?
I could not find any sample code that would show me how to automate Word in Java or Javascript?
Who can point me in the right direction or provide some sample code?
WordMLDocument doc = getDocument(request); //Gets a handle on the WordML document
 
doc.cleanup(); //processes the WordML document
	    
// At this point I would need to create a .doc binary document
 
	    
response.setContentType("application/msword"); //sets the MIME Type
response.getOutputStream().write(doc.getHtmlBytes()); //sends the WordML doc to the user

Open in new window

0
Comment
Question by:tomanizer
  • 3
  • 3
7 Comments
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 500 total points
ID: 20339685
This is fairly complicated.

You'll need to create a Microsoft COM object (and register it) on your server for communicating with Word.  It's that COM object that you'll invoke from your Java program.  The COM object should take a parameter which is the file name of the WordML file.  The COM object can make sure that Word saves the new .doc file to the same location as the WordML file, so you'll know where to find it in your Java program.  Or you can return values from your COM object with the new file location.

Given the significant load caused by invoking Word on your server, you might want to have a standalone Java program which listens for requests to send a WordML file to Word, and which either signals back when it's done, or you would add polling for a certain amount of time -- a minute? -- from your Java servlet to check if the new .doc file exists.  You'll still have the load on your server's cpu, but you won't be invoking the COM object from your Web server, so if something goes wrong with Word or the COM object, you don't bring down your Web server.

Here is an article from Microsoft's perspective on connecting from Java to a COM object:
http://research.microsoft.com/~chadv/java_com2.htm

You could try the com4j package to see if it helps you with integration from java to COM objects:
https://com4j.dev.java.net/

or Jacob:
http://danadler.com/jacob/

Here is Microsoft documentation on using COM objects to attach to Office apps:
http://msdn2.microsoft.com/en-us/library/y1xatbkd(VS.80).aspx
0
 

Author Comment

by:tomanizer
ID: 20339792
Thank you. This is very helpful.
I was hoping to find an easier solution. But if this is is, so be it.

I also found another Java Com Library here: http://sourceforge.net/projects/jcom/

As to your suggestion to do this outside the webserver:
We currently do something similar for pdf docs.
We send our WordML Documents to another server, which opens the doc and prints it to pdf and sends it back to the webserver.
I think I will do the same for the .doc doc.

On the PDF server, we run a simple vbs script, so no java-com bridging is required.

I will try it and close the question and award the points if there are no more comments.
Set oDoc = CreateObject("Word.Application")
oDoc.Documents.Open oArgs(0), False, True
oDoc.ActivePrinter = "CutePDF Writer"
oDoc.ActiveDocument.PrintOut False, , , , , , 0

Open in new window

0
 
LVL 27

Assisted Solution

by:mrcoffee365
mrcoffee365 earned 500 total points
ID: 20339886
It looks as if you're invoking Word from that script -- is that true?  In any case, you can invoke Word from a vbs script, too.

You could then exec the vbs script from a Java program.  Something like:
Process p = Runtime.getRuntime().exec("c:/path/myprog.vbs filename");

Nice article from Sun on exec'ing programs:
http://java.sun.com/developer/JDCTechTips/2003/tt0304.html

That's a lot easier than developing the COM object, although it probably has poorer performance, and gives you a little less control over the execution.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:tomanizer
ID: 20339907
Yes. I am invoking word with that vbs script.
Currently this script runs on another machine.
So there is a bit of haggling with sending the files back and forth.
0
 

Author Comment

by:tomanizer
ID: 20373336
Unfortunately I could not try the solution as the Project Manager decided that all users who want to use our platform should upgrade...

I will award the points to mrcoffee365 as I am confident that the solution would work.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20374967
Thanks, and good luck!
0
 

Expert Comment

by:atharv
ID: 33347583
How can i print the wordml file stored in sql server 2005 database as word document
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 42
hibernate example issues from command prompt 10 50
"Go Fund Me" type plugin 5 24
ejb mdb examples 1 10
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question