|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 zhanglei1371 于 2014-12-23 15:38 编辑
记得下次提问问题一次说清楚,我懒得反复回答一个问题。
代码如下- Sub test1()
- 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
- .Filters.Clear
- .Filters.Add "文本文档", "*.txt", 1
- 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 Application.FileDialog(msoFileDialogFilePicker)
- .AllowMultiSelect = True
- .InitialFileName = ActiveDocument.Path
- .Filters.Clear
- .Filters.Add "文本文档", "*.txt", 1
- .Title = "请选取要锁定的目标文件..."
- If .Show <> -1 Then
- tf = 1
- MsgBox "未选择文件,将对当前文件进行操作!"
- GoTo aa:
- End If
- For Each f In .SelectedItems
- With Documents.Open(f)
- aa:
- 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
- If tf = 1 Then GoTo bb:
- End With
- Documents(f).Close False
- Next
- End With
- bb:
- info = "共搜索到" & c & "条。" & vbCrLf & info
- If a = "-1" Then
- info = "指定单词搜索:" & findtext(0) & vbCrLf & info
- Else
- info = "指定每个单词所需例句的搜索上限数:" & IIf(Val(a) = 0, "全部", a) & vbCrLf & info
- End If
- With Documents.Add
- .Content.Text = info
- .Content.Find.Execute "^12", , , 1, , , , , , "", 2 '去除分页符
- .Content.Find.Execute "^13^13", , , 1, , , , , , "^p", 2 '去除全部空行
- .Content.Find.Execute "^13^13", , , 1, , , , , , "^p", 2 '去除全部空行
- .SaveAs2 "c:\abc.pdf", wdFormatPDF
- .Close False
- End With
- MsgBox "OK ,已生成C:\abc.pdf,路径名称可自己修改!"
- End Sub
- Private Sub CommandButton1_Click()
- Call test1
- End Sub
复制代码
|
|