* "DzemoT. said:
how to read MS Word files with VB.NET ? I'm using IO.StreamReader and work
with *.txt files fine but with *.doc files (MS Word) I get something else.
Word files are stored in a binary format, not in text/plain format.
Nevertheless, you can read the text from a Word file if Word is
installed on the machine:
Add a reference to the "Microsoft Word 10.0 Object Library" COM
component, then use this code (quick and dirty):
\\\
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load
Dim objWord As Word.ApplicationClass
Dim strText As String
objWord = New Word.Application()
With objWord
.Visible = False
.Documents.Open( _
Application.StartupPath & "\cookies.doc", _
, _
True _
)
.WordBasic.EditSelectAll()
.WordBasic.SetDocumentVar( _
"MyVar", _
.ActiveDocument.ActiveWindow.Selection.Text _
)
strText = .WordBasic.GetDocumentVar("MyVar")
TextBox1.Text = _
InsertNewLineChars(Strings.Left(strText, Len(strText) - 1))
.Documents.Close(0)
.Quit()
End With
objWord = Nothing
End Sub
Private Function InsertNewLineChars( _
ByVal strText As String _
) As String
Dim pos As Integer, l As Integer
pos = 1
Do While pos < Len(strText)
l = Strings.InStr(pos, strText, vbCr)
If l = 0 Then Exit Do
strText = _
Strings.Left(strText, l - 1) & vbCrLf & _
Mid(strText, l + 1)
pos = l + 2
Loop
Return strText
End Function
///