COMate: Converting Word-Doc to HTML

Share your advanced PureBasic knowledge/code with the community.
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

COMate: Converting Word-Doc to HTML

Post 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
Hygge
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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?
I may look like a mule, but I'm not a complete ass.
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post 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
Hygge
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Very Nice and oh so very cool!

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