bottom
Great WordTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > WordTips Home > Macros > WordBasic Examples > Counting Characters in a Selection with WordBasic

Counting Characters in a Selection with WordBasic

Summary: Need to figure out the number of characters in a range of selected text? Here's how to do it in WordBasic. (This tip works with Microsoft Word 6, and Word 95.)

When you are developing macros, it is often helpful to know how many characters are in a text selection. The following WordBasic macro, CharCount, will display the number of characters in a selection.

Sub MAIN
Title$ = "CharCount"
On Error Goto HugeString
InSelection = SelType()
If InSelection = 2 Or InSelection = 6 Then
    CharCount = Len(Selection$())
    CharCount$ = Mid$(Str$(CharCount), 2) + " character"
    If CharCount <> 1 Then CharCount$ = CharCount$ + "s"
    MsgBox CharCount$, Title$
Else
    Beep
    MsgBox "No text selected", Title$, 16
End If
Goto Done

HugeString:
    MsgBox "Selection too large", Title$, 48

Done:
End Sub

Tip #745 applies to Microsoft Word versions: 6 | 95


Save Time! WordTips has been published weekly since early 1997. Past issues are available in convenient WordTips archives. Have your own enhanced archive of WordTips at your fingertips, available to use at any time!
 
Check out WordTips Archives today!

Helpful Links

Ask a Word Question
Make a Comment

Tips.Net Home
Tips.Net Store

WordTips FAQ
WordTips Premium

Learn Access Now

Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips

Advertise on the
WordTips Site

 

Great Info!

Get tips like this every week in WordTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)