Word VBA: finding a set of words and inserting predefined comments

て烟熏妆下的殇ゞ 提交于 2019-12-24 13:24:03

问题


I need to automate the insertion of comments into a word document: searching for a predefined set of words (sometimes word strings, and all non case-sensitive) each to which I add a predefined comment.

There are two word sets, with two goals:

  • Wordset 1: identical comment for each located word
  • Wordset 2: individual comments (I suggest new text based on the word identified)

I have been semi-automating this with a code that IDs all identified words and highlights them, helping me through the process (but I still need to enter all the comments manually - and I've also been able to enter comments - but only on one word at a time.) As my VBA skills are limited, my attempts to compile a robust macro from bits of other code with similar purposes has unfortunately led me nowhere.

Below are the bits of code I've been using.

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

The following code has been able to get me to insert bubbles directly

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

I've tried to have the process repeat itself by doing as shown below, but for reasons I'm certain are evident to many of you (and completely unknown to me) - this strategy has failed, working for "word x" but failing to function for all subsequent words:

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

I've mixed and matched bits of these codes to no avail. Any ideas to help me with either wordset?

Thanks for everyone's help!

Best regards


回答1:


Benoit, you're almost there! All you need to do is redefine the range object after your first loop (because it would have been exhausted at that point). Like so:

Sub CommentBubble()
    Dim rng As range
    Set rng = ActiveDocument.Content

    Do While rng.Find.Execute("Word x") = True
        ActiveDocument.Comments.Add rng, "my 1st comment to enter in the bubble"
    Loop

    Set rng = ActiveDocument.Content ' <---------------Add This.

    Do While rng.Find.Execute("Word y") = True
        ActiveDocument.Comments.Add rng, "my 2nd comment to enter in the bubble"
    Loop
End Sub

That should do the trick for you (it works on my end). If not, let me know.



来源:https://stackoverflow.com/questions/37881513/word-vba-finding-a-set-of-words-and-inserting-predefined-comments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!