查找一组单词并插入预定义的注释

查找一组单词并插入预定义的注释

本文介绍了Word VBA:查找一组单词并插入预定义的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要自动将注释插入到Word文档中:搜索预定义的一组单词(有时是单词字符串,并且所有不区分大小写),并向其中添加预定义的注释.

有两个单词集,有两个目标:

  • 单词集1:每个定位单词的注释相同
  • 单词集2:个人评论(我建议根据已识别的单词提出新的文字)

我一直在使用半自动执行此代码,该代码对所有已识别的单词进行ID识别并突出显示它们,从而帮助我完成了该过程(但我仍然需要手动输入所有评论-而且我也能够输入评论-由于我的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

我已尝试通过如下所示的方式重复该过程,但是由于某些原因,我确定对许多人来说都是显而易见的(并且我完全不知道)-该策略失败了,它适用于"word 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

我将这些代码的位混合并匹配都没有用.有什么想法可以帮助我解决这两个单词集吗?

感谢大家的帮助!

最诚挚的问候

解决方案

Benoit,您快要准备好了!您需要做的就是在第一个循环之后重新定义范围对象(因为在那一刻它已经用尽了).像这样:

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

那应该为您解决问题(对我有效).如果没有,让我知道.

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

解决方案

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.

这篇关于Word VBA:查找一组单词并插入预定义的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:29