RobDog888
Jan 9th, 2009, 03:58 AM
There is an easy way to convert a Word Document to a .htm web page by using Words Object Model .SaveAs method and specify the FileFormat argument of .wdFormatHTML
Word 2000 - 2003 and VB.NET 2005 - 2008
Option Explicit On
Option Strict On
'Add a reference to Microsoft Office Word xx.0 Object Library
'Add a Button to your form (Button1)
Imports Microsoft.Office.Interop
Public Class Form1
Dim moApp As Word.Application
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oDoc As Word.Document
Using OpenFileDialog1
With OpenFileDialog1
.CheckFileExists = True
.CheckPathExists = True
.Filter = "Word Documents 2000-2003 Only (*.doc)|*.doc"
.FilterIndex = 1
.Multiselect = False
.ShowReadOnly = False
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
oDoc = DirectCast(moApp.Documents.Open(FileName:=.FileName.ToString), Word.Document)
moApp.Visible = False ' or True if you want to show Word
'Save the doc as the same file name but change the file extension to .htm
oDoc.SaveAs(FileName:=.FileName.ToString.Replace(".doc", ".htm"), FileFormat:=Word.WdSaveFormat.wdFormatHTML)
oDoc.Saved = True
oDoc.Close(SaveChanges:=False)
oDoc = Nothing
'moApp.Visible = False 'If you choose to show Word in previous code
End If
End With
End Using
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not moApp Is Nothing Then
moApp.Quit()
moApp = Nothing
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
moApp = DirectCast(CreateObject("Word.Application"), Word.Application)
Catch ex As Exception
MessageBox.Show(ex.Message, "VB/Office Guru™ Word .NET Demo", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
End Class
Word 2000 - 2003 and VB.NET 2005 - 2008
Option Explicit On
Option Strict On
'Add a reference to Microsoft Office Word xx.0 Object Library
'Add a Button to your form (Button1)
Imports Microsoft.Office.Interop
Public Class Form1
Dim moApp As Word.Application
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oDoc As Word.Document
Using OpenFileDialog1
With OpenFileDialog1
.CheckFileExists = True
.CheckPathExists = True
.Filter = "Word Documents 2000-2003 Only (*.doc)|*.doc"
.FilterIndex = 1
.Multiselect = False
.ShowReadOnly = False
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
oDoc = DirectCast(moApp.Documents.Open(FileName:=.FileName.ToString), Word.Document)
moApp.Visible = False ' or True if you want to show Word
'Save the doc as the same file name but change the file extension to .htm
oDoc.SaveAs(FileName:=.FileName.ToString.Replace(".doc", ".htm"), FileFormat:=Word.WdSaveFormat.wdFormatHTML)
oDoc.Saved = True
oDoc.Close(SaveChanges:=False)
oDoc = Nothing
'moApp.Visible = False 'If you choose to show Word in previous code
End If
End With
End Using
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not moApp Is Nothing Then
moApp.Quit()
moApp = Nothing
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
moApp = DirectCast(CreateObject("Word.Application"), Word.Application)
Catch ex As Exception
MessageBox.Show(ex.Message, "VB/Office Guru™ Word .NET Demo", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
End Class