I'm attempting to read through a Word Document (800+ pages) line by line, and if that line contains certain text, in this case Section
, simply print that line to console.
Public Sub doIt()
SearchFile("theFilePath", "Section")
Console.WriteLine("SHit")
End Sub
Public Sub SearchFile(ByVal strFilePath As String, ByVal strSearchTerm As String)
Dim sr As StreamReader = New StreamReader(strFilePath)
Dim strLine As String = String.Empty
For Each line As String In sr.ReadLine
If line.Contains(strSearchTerm) = True Then
Console.WriteLine(line)
End If
Next
End Sub
It runs, but it doesn't print out anything. I know the word "Section" is in there multiple times as well.
As already mentioned in the comments, you can't search a
Word
document the way you are currently doing. You need to create aWord.Application
object as mentioned and then load the document so you can search it.Here is a short example I wrote for you. Please note, you need to add reference to Microsoft.Office.Interop.Word and then you need to add the import statement to your class. For example
Imports Microsoft.Office.Interop
. Also this grabs each paragraph and then uses the range to look for the word you are searching for, if found it adds it to the list.Note: Tried and tested - I had this in a button event, but put where you need it.
Update to use Sentences - this will go through each paragraph and grab each sentence, then we can see if the word exists... The benefit of this is it's quicker because we get each paragraph and then search the sentences. We have to get the paragraph in order to get the sentences...