Read a word document

  • Thread starter Thread starter Jinu
  • Start date Start date
J

Jinu

Hello

please any idea how to read a formatted word (.doc)
document using vb.net without loosing the format , also
how to insert/add a formated word document into another
word document with out loosing the format


Thank You
 
I am not sure it this is exactly what you want but it will read a word (.doc) document and store its complete formatted contents in a string. I actually use this code to move the strBuffer into a SQL Server 2000 Text (BLOB) to store the doucment. I have a program that then can read the doucment out of the SQL Server 2000 database and display it to the user.

(It took me 8 hours of my life to figure out these 6 lines of code... is'nt programming great!!!)

Hope this helps.

'*-------------------------------------------------------
'* Read Word Document into Byte Array with Binary Reader
'* and convert Byte Array to string.
'*-------------------------------------------------------
Dim BR As New IO.BinaryReader(IO.File.OpenRead(strDocumentFullName))
Dim bArray(intTotalBytes) As Byte
bArray = BR.ReadBytes(intTotalBytes)
Dim strBuffer As String
strBuffer = System.Text.Encoding.Default.GetString(bArray)
BR.Close()

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
Sorry,

I forgot to include the code that figures out the word document file length which is required for the byte array.

Also strDocumentFullName contains name of the word document ("C:\myfiles\testdoc.doc").

The complete code is as follows:


'*-------------------------------------------------------
'* Gets File Size.
'*-------------------------------------------------------
Dim oFile As System.IO.FileInfo
oFile = New System.IO.FileInfo(strDocumentFullName)
Dim oFileStream As System.IO.FileStream = oFile.OpenRead()
Dim intTotalBytes As Int32 = oFileStream.Length
oFileStream.Close()

'*-------------------------------------------------------
'* Read Word Document into Byte Array with Binary Reader
'* and convert Byte Array to string.
'*-------------------------------------------------------
Dim BR As New IO.BinaryReader(IO.File.OpenRead(strDocumentFullName))
Dim bArray(intTotalBytes) As Byte
bArray = BR.ReadBytes(intTotalBytes)
Dim strBuffer As String
strBuffer = System.Text.Encoding.Default.GetString(bArray)
BR.Close()


**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
Back
Top