You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
在当前活动的 Word 文档中搜索指定的文本,并将搜索结果(包含匹配文本所在的页码和匹配文本片段)导出到桌面的一个名为 Search_Results.txt 的文本文件中。
`Sub ExportSearchResultsWithPageNumbers()
Dim searchText As String
Dim rng As Range
Dim pageNum As Integer
Dim filePath As String
Dim fileNum As Integer
Dim outputText As String
Dim matchCount As Integer
searchText = InputBox("请输入要查找的文字:")
If searchText = "" Then Exit Sub
Set rng = ActiveDocument.Content
outputText = ""
matchCount = 0
With rng.Find
.Text = searchText
.Forward = True
.Wrap = wdFindStop
Do While .Execute
' 获取当前匹配项的页码
pageNum = rng.Information(wdActiveEndPageNumber)
' 写入结果:页码 + 匹配文本片段
outputText = outputText & "页码 " & pageNum & ": " & rng.Text & vbCrLf
matchCount = matchCount + 1
rng.Collapse wdCollapseEnd
Loop
End With
If matchCount = 0 Then
MsgBox "未找到匹配项!"
Exit Sub
End If
' 保存结果到文本文件(UTF-8 编码)
filePath = Environ("USERPROFILE") & "\Desktop\Search_Results.txt"
fileNum = FreeFile
Open filePath For Output As #fileNum
Print #fileNum, outputText
Close #fileNum
MsgBox "导出完成!找到 " & matchCount & " 个匹配项。路径:" & filePath
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
在当前活动的 Word 文档中搜索指定的文本,并将搜索结果(包含匹配文本所在的页码和匹配文本片段)导出到桌面的一个名为 Search_Results.txt 的文本文件中。
`Sub ExportSearchResultsWithPageNumbers()
Dim searchText As String
Dim rng As Range
Dim pageNum As Integer
Dim filePath As String
Dim fileNum As Integer
Dim outputText As String
Dim matchCount As Integer
End Sub
`
Beta Was this translation helpful? Give feedback.
All reactions