Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Counting occurence of specific character in selection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ed - 13 Mar 2008 21:18 GMT
Is there a function that counts the number of times a particular character
appears within a selection? Thanks.

Ed (in Virginia)
Karl E. Peterson - 13 Mar 2008 22:54 GMT
> 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

Signature

.NET: It's About Trust!
http://vfred.mvps.org

Ed - 14 Mar 2008 00:13 GMT
Thanks! Works like a charm. ed

>> Is there a function that counts the number of times a particular
>> character
[quoted text clipped - 11 lines]
>   ?ubound(split("abcabcabcab", "c"))
>    3
Ed - 14 Mar 2008 11:48 GMT
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)

>> Is there a function that counts the number of times a particular
>> character
[quoted text clipped - 11 lines]
>   ?ubound(split("abcabcabcab", "c"))
>    3
Jean-Guy Marcil - 14 Mar 2008 15:08 GMT
> Karl (or others):
>
[quoted text clipped - 6 lines]
>
> 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."
Roy - 17 Mar 2008 14:43 GMT
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.

>> Karl (or others):
>>
[quoted text clipped - 25 lines]
>
> MsgBox "The target character was found " & lngFindCount & " times."
Jean-Guy Marcil - 17 Mar 2008 15:33 GMT
> 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.
Karl E. Peterson - 19 Mar 2008 19:14 GMT
> Karl (or others):
>
[quoted text clipped - 4 lines]
> 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.
Signature

.NET: It's About Trust!
http://vfred.mvps.org

Klaus Linke - 19 Mar 2008 21:28 GMT
> (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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.