Group: microsoft.public.word.vba.general
From: =?Utf-8?B?SmVhbi1HdXkgTWFyY2ls?=
Date: Friday, March 14, 2008 9:08 AM
Subject: Re: Counting occurence of specific character in selection

"Ed" wrote:

> Karl (or others):
>
> I am trying to count "forward slashes". "/"
>
> The routine you suggested works perfectly in Word2003. And it works
> perfectly in Word2007 if there are no images. But if there are images within
> the selected text, the images get counted as a slash, messing up the count.
> Is there another technique? (Is this a bug in Word2007)?
>
> Example: Texttexttext/(image1)Texttexttext2/(image2)Texttexttext3
>

Try this:

Dim rgeFind As Range
Dim lngRgeEnd As Long
Dim lngFindCount As Long

Set rgeFind = Selection.Range
lngRgeEnd = rgeFind.End
lngFindCount = 0

Do While rgeFind.Find.Execute(FindText:="\", Wrap:=wdFindStop)
lngFindCount = lngFindCount + 1
Set rgeFind = ActiveDocument.Range(rgeFind.End, lngRgeEnd)
Loop

MsgBox "The target character was found " & lngFindCount & " times."

Safety Articles | Usenet Groups | Usenet News | Bluegrass