• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

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.
0
lll112
Asked:
lll112
  • 3
1 Solution
 
sivaramyCommented:
Use Ole Automation
Create a Word Object

For your first problem
Programmatically open the HTML file in Word.

Invoke the save method and save it as Word Doc.

Second problem

Open the first file

Programmatically merge the other.

To get the codes record a macro and do it manually then convert it.

If you want the exact code let me know!
0
 
stevestringerCommented:
|||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.
0
 
lll112Author Commented:
Thanks for your help.
May I have your source code?
Also, can you write down all the procedures in detail.

Thanks again.
0
 
sivaramyCommented:
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)
0
 
sivaramyCommented:
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
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now