MS Word & .NET?

  • Thread starter Thread starter DzemoT.
  • Start date Start date
D

DzemoT.

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.
Any example '
THX
 
* "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
///
 
You can't read Microsoft Word files directly -- it's a proprietary binary
format. You have several options:

1. Use Office Automation to control Microsoft Word. Samples here:
http://msdn.microsoft.com/vstudio/office/default.aspx

2. Have the user save the Microsoft Word document in an non-proprietary
format, like .txt, .rtf, or Word XML (if using Microsoft Word 2003.)
 
thx
Herfried K. Wagner said:
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
///
 
Back
Top