C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: Please add the Microsoft.Office.Interop.Word assembly to your project. Go to Project -> Add Reference.
Then: We call Documents.Open with the file name of the document specified. Next, we loop through all the words in the document.
For: We loop from the index 1 to the value of Count inclusive. The Text property from the Words collection is accessed each time.
For Each, ForConsoleWord document: word.doc
One
Two
three
VB.NET program that reads doc file
Imports Microsoft.Office.Interop.Word
Module Module1
Sub Main()
' Create application instance.
Dim app As Application = New Application
' Open specified file.
Dim doc As Document = app.Documents.Open("C:\word.doc")
' Loop through all words.
Dim count As Integer = doc.Words.Count
For i As Integer = 1 To count
' Write word to screen.
Dim text As String = doc.Words(i).Text
Console.WriteLine("Word {0} = {1}", i, text)
Next
' Quit the application.
app.Quit()
End Sub
End Module
Output
Word 1 = One
Word 2 =
Word 3 = Two
Word 4 =
Word 5 = three
Word 6 =