-
Jan 9th, 2009, 04:58 AM
#1
[FAQ's: OD] How do I convert a Word Document to a Webpage?
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
VB.NET Code:
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|