|
可试试如下代码:
问题一:- Sub test1()
- Dim findtext As String, n As Integer, info As String
- findtext = InputBox("请输入要查的单词", , "Number")
- If findtext <> "" Then
- With ActiveDocument.Content.Find
- .Text = findtext
- .MatchAllWordForms = True
- Do While .Execute
- n = n + 1
- With .Parent
- .Expand wdSentence
- If .Text Like "*" & Chr(13) Then .End = .End - 1
- info = info & n & vbTab & .Text & Chr(13)
- .Collapse wdCollapseEnd
- End With
- Loop
- End With
- With Documents.Add.Content
- .Text = info
- End With
- End If
- End Sub
复制代码 问题二:- Sub test2()
- Dim fs, f
- Dim Dlg As FileDialog, findtext() As String
- Dim i As Integer, n As Integer, c As Long, info As String
-
- Set fs = CreateObject("Scripting.FileSystemObject")
- Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
- With Dlg
- .InitialFileName = ActiveDocument.Path
- .AllowMultiSelect = False
- If .Show <> -1 Then Exit Sub
- Set f = fs.OpenTextFile(.SelectedItems(1))
- findtext() = Split(f.ReadAll, vbCrLf)
- End With
- With ActiveDocument.Content.Find
- .MatchAllWordForms = True
- For i = 0 To UBound(findtext)
- info = info & vbCrLf & findtext(i) & vbCrLf
- .Text = findtext(i)
- Do While .Execute
- n = n + 1
- c = c + 1
- With .Parent
- .Expand wdSentence
- If .Text Like "*" & Chr(13) Then .End = .End - 1
- info = info & n & vbTab & .Text & Chr(13)
- .Collapse wdCollapseEnd
- End With
- Loop
- .Parent.WholeStory
- n = 0
- Next
- End With
- info = "共搜索到" & c & "条。" & vbCrLf & info
- Documents.Add.Content.Text = info
- End Sub
复制代码 |
|