1

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.

Tronje182
  • 80
  • 1
  • 9
  • 3
    Never say "it's not working" when asking for help. Describe in detail what steps you tried, what output you *expected* from them and what output you *got*. Right now it's not possible to know if your problem is an empty HTML file, and uncaught runtime exception, a file in the wrong encoding, a file where HTML entities are not escaped, etc., and all of these would need different resolutions. – Andrzej Doyle Aug 05 '10 at 11:16
  • Thanks, I've added some details now. – Tronje182 Aug 05 '10 at 11:21

2 Answers2

3

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
  • 13,574
  • 2
  • 45
  • 55
  • 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 Aug 05 '10 at 11:27
  • 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 Aug 05 '10 at 13:26
  • 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 Aug 11 '10 at 06:58
3

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
  • 80
  • 1
  • 9
  • 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. – Todd Main Aug 05 '10 at 13:39
  • 1
    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 Aug 06 '10 at 06:40