搜索语料的宏代码
Public Sub Finder()
‘声明原始变量
Dim a$
‘记录所要搜索的字符串
Dim count_ ‘记录字符串出现的次数
‘使用通配符
Selection.Find.ClearFormatting
With Selection.Find
.Text = “”
.Replacement.Text = “”
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
End With
Selection.Find.Execute
‘光标移至文档开始
Selection.HomeKey Unit:=wdStory
‘显示输入对话框,将需要搜索的字符串赋给变量a$
a$
= WordBasic.[InputBox$](”查找一个词并将上下文复制在文挡2上。请填入欲找的词”, “查找”)
WordBasic.EditFind Find:=a$
‘开始搜索操作循环
While WordBasic.EditFindFound() ‘如果发现目标,则开始下面的操作
count_ = count_ + 1
’计数器加一
WordBasic.SentRight 1
‘下面三句:选中字符串所在的一行
WordBasic.SentLeft 1
WordBasic.SentRight 1, 1
WordBasic.EditCopy
‘复制该行入剪贴板
WordBasic.NextWindow
‘将焦点
转移至下一个Word文档
WordBasic.Insert “
“
‘插入
两个空格
WordBasic.EditPaste
‘粘贴剪贴板的内容
WordBasic.InsertPara
‘换行
WordBasic.NextWindow
‘回到原来的Word文档
WordBasic.CharLeft 1
WordBasic.CharRight 1
WordBasic.SentRight 1
WordBasic.EditFind
Wend ’循环结束
‘在文档中输出搜索结果
WordBasic.NextWindow ‘将焦点
转移至下一个Word文档
WordBasic.InsertPara ‘换行
WordBasic.Insert “[包含”
‘以下七句:输出字符串出现的次数
WordBasic.Insert “” + Chr(-24144) + “”
WordBasic.Insert a$
WordBasic.Insert “” + Chr(-24143) + “”
WordBasic.Insert “的句子出现次数:”
WordBasic.Insert Str(count_)
WordBasic.Insert “]”
WordBasic.NextWindow ‘将焦点
转移至下一个Word文档
End Sub