Link to home
Start Free TrialLog in
Avatar of lll112
lll112

asked on

Word Docs to HTML converter

How to convert a html file to a word doc files programmically?
How to combine two word doc files to one word doc file?

I want a full description how to do it.
Thanks alot.
ASKER CERTIFIED SOLUTION
Avatar of sivaramy
sivaramy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stevestringer
stevestringer

|||112

The verbose information as requested.

Create a new VB project and add in a reference to Word 8.0 Automation Library.  The below code is fully commented and demonstrates how to achieve the full approach soley within VB.

Copy this code and place under a Command1_Click event on a temp form to see how it works.  Please remember to change the document names used to ones that you have.  This example shows to different sections - one converting HTML to word documents and the other merging it.  

'************** CODE START ***********
'Declarations
Dim objWord As Word.Application
Dim objWordDoc_One As Word.Document
Dim objWordDoc_Two As Word.Document
Dim objWordDoc_Three As Word.Document
Dim objWordFileConvertor As Word.FileConverter

'Open an instance of Word - modify to use existing
Set objWord = New Word.Application
objWord.Visible = True

'----------------------------------------------------------------
'THIS CODE DEMONSTRATES CONVERTING AN HTML FILE INTO A WORD FILE
'----------------------------------------------------------------
'Load the html document.  Modify the filename to point to an existing html document to convert
Set objWordDoc_One = objWord.Documents.Open(filename:="h:\ss\html document.html")

'Modify the file format when saving
objWordDoc_One.SaveAs filename:="h:\ss\html document.doc", FileFormat:=0
objWordDoc_One.Close
Set objWordDoc_One = Nothing

MsgBox "The HTML document has been loaded and saved as a standard word document"

'---------------------------------------------------------------------------
'THIS CODE DEMONSTRATES JOINING THE CONTENTS OF TWO WORD DOCUMENTS TOGETHER
'---------------------------------------------------------------------------
'Load the first source document and save as the target merge document
Set objWordDoc_One = objWord.Documents.Open(filename:="h:\ss\html document.doc")
objWordDoc_One.SaveAs filename:="h:\merged document.doc"
objWordDoc_One.Close

'Now load the other document to be merged
Set objWordDoc_One = objWord.Documents.Open(filename:="h:\ss\word document.doc")

'Merge this into the merge document created above
objWordDoc_One.Merge "h:\ss\merged document.doc"

objWordDoc_One.Close
Set objWordDoc_One = Nothing
objWord.Quit
Set objWord = Nothing

'************** CODE END ***********

If you require any further help please let me know.
Avatar of lll112

ASKER

Thanks for your help.
May I have your source code?
Also, can you write down all the procedures in detail.

Thanks again.
Thanks for accepting my answer. I think SteveStringer has given the complete code.

Anyway I'll also post my solution soon (It will be almost same like SteveStringer's)
For Both these tasks you have to set the reference to Word Object Library. To do this:

Choose Project->References-> Microsoft Word 8.0 Object Library (for Office 97)
                        Microsoft Word 9.0 Object Library (for Office 2000)

*****************************************************************************************

Saving HTML File AS Word Document

Create a form and put a command button named Command1 and copy this code

The Location, Source, Target are hardcoded you have to change them according to your requirements.

Private Sub Command1_Click()
    Dim oWord As Word.Application
    Dim oSourceHTML As Word.Document
    Dim cLocation As String
    Dim cSourceFile As String
    Dim cTargetFile As String
   
    cLocation = "C:\Temp\"
    cSourceFile = "q125967.html"
    cTargetFile = "q125967.doc"
   
    ' Create a word object instance
    Set oWord = New Word.Application
   
    ' Open the HTML File
    Set oSourceHTML = oWord.Documents.Open(FileName:=cLocation + cSourceFile)
   
    ' Save it as Word document

    oSourceHTML.SaveAs FileName:=cLocation + cTargetFile, FileFormat:= _
        wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
        True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
        False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False
   
   
    'Close the saved document
    oSourceHTML.Close
   
    ' Close Word
    oWord.Quit
   
    'Clear the variables
    Set oSourceHTML = Nothing
   
    Set oWord = Nothing

    MsgBox "Finished"

End Sub

*****************************************************************************************

Merging Two Files

Create a form and put a command button named Command2 and copy & Paste this code

The Location, File1, File2 Target are hardcoded you have to change them according to your requirements.

Private Sub Command2_Click()
   
    Dim oWord As Word.Application
    Dim oMergeFile As Word.Document
    Dim cLocation As String
    Dim cFile1 As String
    Dim cFile2 As String
    Dim cTargetFile As String
   
    cLocation = "C:\Temp\"
    cFile1 = "q125967.doc"
    cFile2 = "q133163.doc"
    cTargetFile = "Merged.doc"
   
    Set oWord = New Word.Application
   
    'Open the First File
    Set oMergeFile = oWord.Documents.Open(FileName:=cLocation + cFile1, ConfirmConversions:=False, _
        ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto)
   
    'Save the first file as merged file name
    oMergeFile.SaveAs FileName:=cLocation + cTargetFile, FileFormat:=wdFormatDocument, _
         LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
        :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
        SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
        False
   
    'Here I am using Insert File Method or else you can ue the "Merge" method as well
   
    'Navigate to the end
    oWord.Selection.EndKey Unit:=wdStory
   
    'Insert The second File
    oWord.Selection.InsertFile FileName:=cLocation + cFile2, Range:="", _
        ConfirmConversions:=False, Link:=False, Attachment:=False
   
    oMergeFile.Save
   
    oMergeFile.Close
   
    oWord.Quit
   
    Set oMergeFile = Nothing
   
    Set oWord = Nothing
   
    MsgBox "Finished"
   
End Sub

*****************************************************************************************

Better way to learn this is record a macro in word and convert the code to VB.

If you want the complete code in VB format send a mail to ee_support@sivaram.8m.com