Solved

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

Posted on 2007-11-23
7
1,854 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now