VBA Do While same bubble comment for multiple string matches - Microsoft Community


hi, have large string of terms , phrases, , looking match each occurrence same bubble comment.  i able post 1 comment 1 find,  and post 1 comment multiple string finds in selection.range.  the matches exact , isolated original selection.range, matchcase = false.  the string in case "mywordsone" comment of "my comment text".  i understand while, range (maybe two), , counter may involved, , maybe collapse, unable figure out working combination.   

any or link appreciated.  is possible selection.range fixed start , end point? 

a sample sentence "this sentence has of examples driving force condone, driving factor."  for mywordsone = split("all,all way," _

& "done, driving force", ",")

with bubble comment of "my comment text" each of "all, driving force".

thanks,

gar

sub comments_bubble_bulk()

dim long
dim mywordsone variant
dim rng range
dim rngdup range
set rng = selection.range
set rngdup = selection.range
selection.find.clearformatting

'sample string

mywordsone = split("all,all way," _
& "done, driving force", ",")
= 0 ubound(mywordsone)
rng.find
    while .execute(findtext:=mywordsone(i), matchwholeword:=true, matchwildcards:=false, _
        forward:=true, wrap:=wdfindstop) = true
            selection.comments.add range:=rngdup, text:="my comment text"
    loop
end with
next i
end sub

***post moved moderator appropriate forum category.***

tweaked code further match conditions to resolve original request.  thank stefan blom, jay freedman, , doug robbins generous leadership , advice.

pros:

1.  matchcase = false.  way, can enter 1 set of key terms, match "all" or "all" or "all" 'all'.

2.  matches key terms exactly, no matches left or right extensions.  example, not matching  "hall" 'all' , not matching "driving forcefully" 'driving force'.

3.  key terms (mywordsone) must listed in reverse alphabetical order, example, z a. example, 'all way' precede 'all'.

4.  bubble comments added key term matches in original selection.range only.

5.  matches key terms standalone, @ beginning of sentence or after paragraph mark, before or after space, before comma or colon or semicolon or period, inside parentheses, or inside single or double curly quotation marks.  see "if rngdup.text = " " or " section for user additions or deletions.

6.  fast.

cons:

the bubble comment default color covers previous highlighting of matching key terms.

-----

dim long
 dim mywordsone variant
 dim rng range, rngdup range
 dim findrange range
  set findrange = selection.range
      mywordsone = split("driving force,done," _
          & "all way,all", ",")
     = 0 ubound(mywordsone)
      selection.homekey wdstory
      selection.find.clearformatting
      selection.find
          while .execute(findtext:=mywordsone(i), _
              forward:=true, wrap:=wdfindstop, matchwildcards:=false, _
              matchwholeword:=true, matchcase:=false, matchallwordforms:=false, _
              matchsoundslike:=false, format:=true) = true
              selection
                 if .start >= findrange.start , .end <= findrange.end then
                     set rng = .range
                     set rngdup = rng.duplicate
                     rngdup.collapse wdcollapsestart
                     rngdup.start = rngdup.start - 1
                     if rngdup.text = " " or rngdup.text = "," _
                         or rngdup.text = chr(40) or rngdup.text = chr(145) or _
                         rngdup.text = chr(13) or rngdup.text = chr(147) then
                       if len(rngdup.text) = len(mywordsone(i)) then
                            activedocument.comments.add range:=rng, text:="my comment text "
                        end if
                        if rngdup.text = " " or rngdup.text = "," _
                         or rngdup.text = chr(40) or rngdup.text = chr(145) or _
                         rngdup.text = chr(13) or rngdup.text = chr(147) then
                             set rngdup = rng.duplicate
                             rngdup.collapse wdcollapseend
                             rngdup.end = rngdup.end + 1
                             if rngdup.text = " " or rngdup.text = "," or _
                                 rngdup.text = "." or rngdup.text = ";" _
                                 or rngdup.text = ":" or rngdup.text = chr(41) or _
                                 rngdup.text = chr(146) or rngdup.text = chr(148) then
                            activedocument.comments.add range:=rng, text:="my comment text "
                           end if
                         end if
                       end if
                    .collapse wdcollapseend
                 end if
              end with
          loop
      end with
  next i

------

thanks again all,

garsul



Office / Word / Microsoft Office Programming / Unknown/other



Comments

Popular posts from this blog

Can't Sign-in to OneDrive - Microsoft Community

Shared mailbox permissions - Microsoft Community

Outlook 2016 for Mac - Multiple Issues, Error 17884 - Microsoft Community