开发者

WORD VBA Count Wildcard Search Hits

开发者 https://www.devze.com 2023-02-21 05:52 出处:网络
Hi i have the codes below that is already working perfectly. What i can\'t figure out is how to count the number of times a wildcard search finds a match. Can anyone tell me how i can go about this? T

Hi i have the codes below that is already working perfectly. What i can't figure out is how to count the number of times a wildcard search finds a match. Can anyone tell me how i can go about this? The codes are a开发者_Python百科s follows:

Sub findfunction()
If (findHL(activedocument.Content, "[aeiou]")) = True Then MsgBox "Highlight vowels   Done", vbInformation + vbOKOnly, "Vowels Highlight Result"
If (findHL(activedocument.Range, "<wa*>")) = True Then MsgBox "Highlight words beginning with WA", vbInformation + vbOKOnly, "Prefix Find Result"
End Sub

Function findHL(r As Range, s As String) As Boolean
Options.DefaultHighlightColorIndex = wdRed
r.Find.Replacement.highlight = True
r.Find.Execute FindText:=s, MatchWildcards:=True, Wrap:=wdFindContinue, Format:=True,  replace:=wdReplaceAll

findHL = True
End Function

Any help would greatly be appreciated. Thanks guys!


I think you just need a static Counter field defined at the top of your module - i.e. not within any sub or function (which is what makes it static):

dim MatchCounter as long

Just initialize to zero at the appropriate place and have your matching function increment on each match. (Could also wrap this in a small Class if the initialization logic / update logic is spread around & hard to pin down).

0

精彩评论

暂无评论...
验证码 换一张
取 消