【发布时间】:2017-03-10 21:05:03
【问题描述】:
我正在为 Word 开发一个宏,该宏可以访问一个单独保存的 doc 文件,该文件包含几页的长单词列表。单词列表文档的格式如下,
FMS
心肺复苏术
Abc
...用换行符分隔每个单词。
宏需要突出显示列表中每个单词的第一次使用。
现在,宏会突出显示单词的每一次使用,此外,当它是另一个单词的一部分时会突出显示该单词。例如,它在单词 freeze 中突出显示 EZE,但它应该只在 eze 单独出现时突出显示。
有人可以帮忙吗? 1. 仅突出首次使用,并且 2.如何确保它只捕获实际单词,而不是包含该单词的所有其他单词?我似乎无法用 VBA 做到这一点。 我当前的代码:
Sub TD()
'
Dim sCheckDoc As String
Dim docRef As Document
Dim docCurrent As Document
Dim wrdRef As String
Dim wrdPara As Paragraph
sCheckDoc = "c:\check.docx"
Set docCurrent = Selection.Document
Set docRef = Documents.Open(sCheckDoc)
docCurrent.Activate
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Replacement.Text = "^&"
.Forward = True
.Format = True
.MatchWholeWord = True
.MatchCase = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
For Each wrdPara In docRef.Paragraphs
wrdRef = wrdPara.Range.Text
If Asc(Left(wrdRef, 1)) > 32 Then
' remove the paragraph mark:
wrdRef = Left(wrdRef, Len(wrdRef) - 1)
With Selection.Find
.Wrap = wdFindContinue
.Text = wrdRef
.Execute Replace:=wdReplaceAll
End With
End If
Next wrdPara
docRef.Close
docCurrent.Activate
End Sub
【问题讨论】: