tags:

views:

65

answers:

2

Hello, i'm trying to make a simple Java program to open an existing word-document, change something and save it as .html-file.

The part which is not working is to save it as .html . The problem is, i got the html-file but it's only a renamed doc-file. So not really a .html-file which I can work with.

This is what I found with Google:

Object oWordBasic = Dispatch.call(oWord, "WordBasic").getDispatch(); 
Dispatch.call((Dispatch) oWordBasic, "FileSaveAs", path); 

What I have to do, to get a html-file as output?

Thank you in advance.

+2  A: 

It's using the OLE Automation Object to save the file, so you have to find the method or parameter to indicate filetype.

This is the macro I could record using Word:

ActiveDocument.SaveAs filename:="asdd.htm", FileFormat:=wdFormatHTML, _
    LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
    :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
    SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
    False

So it means you have to indicate FileFormat := wdFormatHTML (or the constant value) parameter to the SaveAs method. That's left as an exercise to the reader :)

helios
PS: you always can try recording a macro in Word, looking at the generated code and learn how you do something vía VBA. Next you can translate that VBA code to your real code (VBScript, or Dispatch.call's in your case).
helios
Thanks, i found the answer.I've tried it with the macro function of word before asking the question, but it didn't got me any further ;)
Tronje182
Most of the time the values a user change become parameters of a method. So in this case you have to choose file type to write a valid HTML. And it became a parameter of the method :) I found very interesting and powerful to integrate VBA into some other application, enjoy.
helios
+1  A: 

I figured it out, thanks to helios for the tip.

The correct code is:

Object oWordBasic = Dispatch.call(oWord, "WordBasic").getDispatch(); 
Dispatch.call((Dispatch) oWordBasic, "FileSaveAs", path, new Variant(8)); 

The Parameter of the variant is the output format. (for example 8 is html, 6 is rtf, 17 is pdf) You can find the full list at: WdSaveFormat Enumeration

Tronje182
Great, since helios answer was the most helpful in getting your issue resolved, you can click the hollow checkmark next to it to accept it.
Otaku
You could declare the same constant in your Java program to keep the meaning of 8 clear. And must be a way to make a named parameters call, I mean, to do the `method param1=value1,param5=value5` thing :). Thanks for the check!
helios