|
现在程序是每次列出一个例句,能不能添加一个对话框,在程序执行前,可以自由输入想要给每个单词配几个句子。比如输入3,就会给单词列表里的单词配三个例句。
楼主的要求真是层出不穷啊。
以下代码将前面的相关代码进行了组合,以满足多种需要。但毕竟程序不可能完美,特别是对动词短语的搜索,请楼主将就着用吧。- Sub test6()
- Dim a As String
- Dim fs, f
- Dim findtext() As String, temp As String
- Dim i As Integer, n As Integer, c As Long, info As String
-
- a = InputBox("请输入每个单词所需要的例句数。其中:" & vbCrLf & vbCrLf & _
- "0 全部列出(默认)" & vbCrLf & "-1 指定一个单词搜索", , 0)
- If a = "" Then Exit Sub
- If a = "-1" Then
- ReDim findtext(0)
- findtext(0) = InputBox("请输入要查的单词", , "Number")
- Else
- Set fs = CreateObject("Scripting.FileSystemObject")
- With Application.FileDialog(msoFileDialogFilePicker)
- .Title = "请指定单词列表文本文件"
- .InitialFileName = ActiveDocument.Path
- .AllowMultiSelect = False
- If .Show <> -1 Then Exit Sub
- Set f = fs.OpenTextFile(.SelectedItems(1))
- findtext() = Split(f.ReadAll, vbCrLf)
- f.Close
- Set fs = Nothing
- End With
- End If
-
- With ActiveDocument.Content.Find
- For i = 0 To UBound(findtext)
- .MatchAllWordForms = IIf(findtext(i) Like "*[!A-Za-z]*", False, True)
- info = info & vbCrLf & findtext(i) & vbCrLf
- .Text = findtext(i)
- Do While .Execute
- n = n + 1
- c = c + 1
- If Val(a) > 0 And n > Val(a) Then Exit Do
- With .Parent
- .Expand wdSentence
- temp = .Text
- If Right(temp, 1) <> Chr(13) Then temp = temp & vbCrLf
- If Val(a) = 1 Then
- If InStr(info, vbCrLf & temp) = 0 Then
- info = info & temp
- Else
- info = Replace(info, vbCrLf & findtext(i) & vbCrLf, "")
- info = Replace(info, vbCrLf & temp, "|" & findtext(i) & vbCrLf & temp)
- c = c - 1
- End If
- Exit Do
- Else
- If InStr(info, vbTab & temp) > 0 Then temp = "*" & temp '用星号标记重复的例句
- info = info & n & vbTab & temp
- End If
- .Collapse wdCollapseEnd
- End With
- Loop
- n = 0
- .Parent.WholeStory
- Next
- End With
-
- info = "共搜索到" & c & "条。" & vbCrLf & info
- If a = "-1" Then
- info = "指定单词搜索:" & findtext(0) & vbCrLf & info
- Else
- info = "指定每个单词所需例句的搜索上限数:" & IIf(Val(a) = 0, "全部", a) & vbCrLf & info
- End If
- Documents.Add.Content.Text = info
- End Sub
复制代码 |
|