Page 1 of 1

COMate: Converting Word-Doc to HTML

Posted: Fri Dec 19, 2008 9:29 am
by Kiffi

Code: Select all

; Word-Doc to HTML

IncludePath #PB_Compiler_Home + "comate\"
XIncludeFile "COMate.pbi"

EnableExplicit

#wdFormatHTML         =  8
#wdFormatFilteredHTML = 10
  
Procedure GetFormatHTML(WordApplication.COMateObject)
  
  ; inspired by Joost Schwider
  ; http://vb-tec.de/wordhtml.htm
  
  Protected FileConverter.COMateObject
  Protected FileConverters.COMateEnumObject
  Protected SaveFormat
  
  Debug WordApplication\GetIntegerProperty("Version")
  
  If WordApplication\GetIntegerProperty("Version") > 8 
    SaveFormat = #wdFormatHTML
  Else
    
    FileConverters = WordApplication\CreateEnumeration("FileConverters")
    
    If FileConverters
      
      FileConverter = FileConverters\GetNextObject()
      While FileConverter
        If FileConverter\GetStringProperty("ClassName") = "HTML"
          
          SaveFormat = FileConverter\GetIntegerProperty("SaveFormat")
          
          FileConverter\Release()
          Break
          
        EndIf
        FileConverter\Release()
        FileConverter = FileConverters\GetNextObject()
      Wend
      FileConverters\Release()
    EndIf
    
  EndIf
  
  ProcedureReturn SaveFormat
  
EndProcedure

Procedure ShowErrorIfAny()
  
  If COMate_GetLastErrorCode()
    Debug COMate_GetLastErrorDescription()
  EndIf
  
EndProcedure
            
Procedure Doc2Html(Infile.s, OutFile.s)
  
  Protected ReturnValue
  Protected WordApplication.COMateObject
  
  ReturnValue = #False
  
  If FileSize(Infile) = -1
    Debug "file not found"
  Else
    
    WordApplication = COMate_CreateObject("Word.Application")
    
    If WordApplication
      
      If WordApplication\Invoke("Documents\Open('" + Infile + "')") = #S_OK
        If WordApplication\Invoke("ActiveDocument\SaveAs('" + OutFile + "', " + Str(GetFormatHTML(WordApplication)) + " As Long)") = #S_OK
          ReturnValue = #True
        Else
          Debug "!ActiveDocument\SaveAs"
          Debug ShowErrorIfAny()
        EndIf
        WordApplication\Invoke("Quit(0)")
      Else
        Debug "!Documents\Open"
      EndIf
      
      WordApplication\Release()
      
    Else
      
      Debug "!WordApplication"
      Debug ShowErrorIfAny()
      
    EndIf
    
  EndIf
  
  ProcedureReturn ReturnValue
  
EndProcedure

Define Infile.s  = "D:\Test1.doc"
Define OutFile.s = "D:\fromTest1Doc.html"

If Doc2Html(Infile, OutFile) = #True  
  RunProgram(OutFile)
EndIf
Greetings ... Kiffi

Posted: Fri Dec 19, 2008 11:04 am
by srod
Kiffi, as the author of COMate, I officially award you 'COMate expert' status! :wink:

Very nice - can I add this code to the COMate package?

Posted: Fri Dec 19, 2008 1:36 pm
by Kiffi
srod wrote:I officially award you 'COMate expert' status! :wink:
thanks! :D
srod wrote:can I add this code to the COMate package?
yes, of course you can.

Greetings ... Kiffi

Posted: Fri Dec 19, 2008 4:56 pm
by SFSxOI
Very Nice and oh so very cool!

Thank You for posting this Kiffi, I even have a use for it I think. :)