Counting occurence of specific character in selection

E

Ed

Is there a function that counts the number of times a particular character
appears within a selection? Thanks.

Ed (in Virginia)
 
K

Karl E. Peterson

Ed said:
Is there a function that counts the number of times a particular character
appears within a selection? Thanks.

Get the selection into a string (I'm not an object model guy; I just do code <g>),
then something like this oughta tell ya:

TheCount = UBound(Split(Selection, TheChar))

For example:

?ubound(split("l;kjwet;kew", "c"))
0
?ubound(split("abcabcabcab", "c"))
3
 
E

Ed

Thanks! Works like a charm. ed


Karl E. Peterson said:
Get the selection into a string (I'm not an object model guy; I just do
code <g>), then something like this oughta tell ya:

TheCount = UBound(Split(Selection, TheChar))

For example:

?ubound(split("l;kjwet;kew", "c"))
0
?ubound(split("abcabcabcab", "c"))
3
 
E

Ed

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

Ed (in Virginia)
 
J

Jean-Guy Marcil

Ed said:
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."
 
R

Roy

Sorry, but same error. Apparently the XML coding is sticking into the mix
the very character that I am trying to count. Works fine in XP and 2003
documents. Fails with XML (2007) documents.
 
J

Jean-Guy Marcil

Roy said:
Sorry, but same error. Apparently the XML coding is sticking into the mix
the very character that I am trying to count. Works fine in XP and 2003
documents. Fails with XML (2007) documents.

Sorry, but I can't test my code as I do not have access to 2007 right now.

But, for others who can test code with 2007, when you state "but same
error", are you still referring to the fact that the images are being counted
as a slash, thus throwing off the count? Or is there a new error that came up
when you tried it with XML documents? (In your previous post you mentioned
images, now you are writing about XML documents).

You could add code to count the number of inlineshapes in the range and then
subtract that amount from the total my code (or Karls's code) returns.
 
K

Karl E. Peterson

Ed said:
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)?

As I suggested, you'll need to reduce the selection to *text*, for my trick to work.
That's an object model issue; one I'm not all that familiar with. There's gotta be
some simple method, though. The images, obviously, are not text.
 
K

Klaus Linke

Karl E. Peterson said:
(Is this a bug in Word2007)?

Yes, and a pretty silly one, given that there had been wishes in previous
versions that, instead of Ascii 1, a more sensible Unicode character might
be used ... private, or U+FFFC = "object replacement character", or
whatever... but *not* a "/".

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top