我需要自动将注释插入到word文档中:搜索一组预定义的单词(有时是单词字符串,所有不区分大小写),每个单词都添加了一个预定义的注释。
有两个单词集,有两个目标:
我已经使用代码识别所有已识别的单词并突出显示这些代码进行半自动化,帮助我完成整个过程(但我仍然需要手动输入所有注释 - 而且我也能够输入注释 - 但是,由于我的VBA技能有限,我尝试从具有类似目的的其他代码中编译一个强大的宏,但遗憾的是,我无处可去。
以下是我一直在使用的代码。
Sub HighlightWordList()
Dim range As range
Dim i As Long
Dim TargetList
TargetList = Array("word1", "word2", "word3")
For i = 0 To UBound(TargetList)
Set range = ActiveDocument.range
With range.Find
.Text = TargetList(i)
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute(Forward:=True) = True
range.HighlightColorIndex = wdYellow
Loop
End With
Next
End Sub
以下代码可以让我直接插入气泡
Sub CommentBubble()
'
'
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("Word x") = True
ActiveDocument.Comments.Add range, "my comment to enter in the bubble"
Loop
End Sub
我试图通过如下所示的方式重复这个过程,但由于我确信你们很多人(我完全不知道)的原因 - 这个策略失败了,为“x字”工作“但未能为所有后续词语发挥作用:
Sub CommentBubble()
'
'
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("Word x") = True
ActiveDocument.Comments.Add range, "my 1st comment to enter in the bubble"
Loop
Do While range.Find.Execute("Word y") = True
ActiveDocument.Comments.Add range, "my 2nd comment to enter in the bubble"
Loop
End Sub
我混合并匹配这些代码的部分无济于事。有任何想法可以帮助我解决这两个问题吗?
感谢大家的帮助!
祝你好运
答案 0 :(得分:0)
Sub CommentBubble()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("Word x") = True
ActiveDocument.Comments.Add range, "my 1st comment to enter in the bubble"
Loop
Set range = ActiveDocument.Content ' <---------------Add This.
Do While range.Find.Execute("Word y") = True
ActiveDocument.Comments.Add range, "my 2nd comment to enter in the bubble"
Loop
End Sub
那应该为你做的伎俩(它适用于我的目的)。如果没有,请告诉我。