Code for read DOC file and save as HTML, text , Word XML File
Here is the code that helps to read any document (like .doc document file ) from specified location. This is a web based application and this code is written in VB.NET class as code behind in ASP.Net 2.0, where the word document is hard to upload from client side. Here is the code that uploads the document file and stores as HTML, text , Word XML File
--------------------------------------------------------------------------
Imports System
Imports System.ComponentModel
Imports Microsoft.Office.Interop
Public Class WordAppl
Private oWordAppl As Word.ApplicationClass
Private oDoc As Word.Document
Public Sub New()
oWordAppl = New Word.ApplicationClass
End Sub
' Open a file (the file must exists) and activate it
Public Sub Open(ByVal strFileName As String)
Dim fileName As Object = strFileName
Dim readOnly1 As Object = False
Dim isVisible As Object = True
Dim missing As Object = System.Reflection.Missing.Value
oDoc = oWordAppl.Documents.Open(fileName, missing, readOnly1, missing, missing, missing, missing, missing, missing, missing, missing, isVisible)
oDoc.Activate()
End Sub
Public Sub Open()
Dim missing As Object = System.Reflection.Missing.Value
oDoc = oWordAppl.Documents.Add(missing, missing, missing, missing)
oDoc.Activate()
End Sub
Public Sub Quit()
Dim missing As Object = System.Reflection.Missing.Value
oWordAppl.Application.Quit(missing, missing, missing)
End Sub
Public Sub Save()
oDoc.Save()
End Sub
Public Sub SaveAs(ByVal strFileName As String)
Dim missing As Object = System.Reflection.Missing.Value
Dim fileName As Object = strFileName
Dim docType As Integer
docType = 2 ' Read doc file as Txt file
' docType = 10 ' Read doc file as HTML file
' docType = 11 ' Read doc file as XML file
oDoc.SaveAs(fileName, 2) ', missing, missing, missing, missing, missing, missing, missing, missing, missing)
End Sub
Public Sub FindAndReplacement(ByVal strFind As String, ByVal strReplace As String)
Try
oWordAppl.Selection.HomeKey(Word.WdUnits.wdStory, Word.WdMovementType.wdExtend)
With oWordAppl.Selection.Find
.Text = strFind
With .Replacement
.Text = strReplace
End With
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
Catch ex As Exception
'ClientUtilities.ShowErrorMessage(ex)
End Try
End Sub
End Class
----------------------------------------------------------------------
use in your code behind page.
'for Open a word document.
WordAppl.Open('Word File name')
'for save file as text
WordAppl.save(server.mappath("/") & "/" & "filename")
see other information related to aspnet35 blogspot site