我正在尝试逐行阅读 Word 文档(800 多页),如果该行包含某些文本(在本例中为 Section
),只需将该行打印到控制台即可。
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
它运行了,但没有打印出任何东西。我知道“部分”这个词也多次出现在其中。
最佳答案
正如评论中已经提到的,您无法按照当前的方式搜索 Word
文档。您需要如上所述创建一个 Word.Application
对象,然后加载文档以便您可以搜索它。
这是我为您编写的一个简短示例。请注意,您需要添加对 Microsoft.Office.Interop.Word 的引用,然后需要将导入语句添加到您的类中。例如导入 Microsoft.Office.Interop
。此外,这会抓取每个段落,然后使用范围来查找您正在搜索的单词,如果找到,会将其添加到列表中。
注意:经过尝试和测试 - 我将其放在按钮事件中,但将其放在您需要的位置。
Try
Dim objWordApp As Word.Application = Nothing
Dim objDoc As Word.Document = Nothing
Dim TextToFind As String = YOURTEXT
Dim TextRange As Word.Range = Nothing
Dim StringLines As New List(Of String)
objWordApp = CreateObject("Word.Application")
If objWordApp IsNot Nothing Then
objWordApp.Visible = False
objDoc = objWordApp.Documents.Open(FileName, )
End If
If objDoc IsNot Nothing Then
'loop through each paragraph in the document and get the range
For Each p As Word.Paragraph In objDoc.Paragraphs
TextRange = p.Range
TextRange.Find.ClearFormatting()
If TextRange.Find.Execute(TextToFind, ) Then
StringLines.Add(p.Range.Text)
End If
Next
If StringLines.Count > 0 Then
MessageBox.Show(String.Join(Environment.NewLine, StringLines.ToArray()))
End If
objDoc.Close()
objWordApp.Quit()
End If
Catch ex As Exception
'publish your exception?
End Try
更新为使用句子 - 这将遍历每个段落并抓取每个句子,然后我们可以查看该单词是否存在...这样做的好处是速度更快,因为我们获取每个段落并且然后搜索句子。我们必须先获取段落才能获取句子...
Try
Dim objWordApp As Word.Application = Nothing
Dim objDoc As Word.Document = Nothing
Dim TextToFind As String = "YOUR TEXT TO FIND"
Dim TextRange As Word.Range = Nothing
Dim StringLines As New List(Of String)
Dim SentenceCount As Integer = 0
objWordApp = CreateObject("Word.Application")
If objWordApp IsNot Nothing Then
objWordApp.Visible = False
objDoc = objWordApp.Documents.Open(FileName, )
End If
If objDoc IsNot Nothing Then
For Each p As Word.Paragraph In objDoc.Paragraphs
TextRange = p.Range
TextRange.Find.ClearFormatting()
SentenceCount = TextRange.Sentences.Count
If SentenceCount > 0 Then
Do Until SentenceCount = 0
Dim sentence As String = TextRange.Sentences.Item(SentenceCount).Text
If sentence.Contains(TextToFind) Then
StringLines.Add(sentence.Trim())
End If
SentenceCount -= 1
Loop
End If
Next
If StringLines.Count > 0 Then
MessageBox.Show(String.Join(Environment.NewLine, StringLines.ToArray()))
End If
objDoc.Close()
objWordApp.Quit()
End If
Catch ex As Exception
'publish your exception?
End Try
关于VB.Net:按行搜索Word文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41471451/