Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Character count with macro (Word 2003)

    Hi,
    I've recorded the following macro in Word:

    Sub count()
    '
    ' count Macro
    ' Macro recorded 12.11.2008 by User
    '
    Selection.MoveRight Unit:=wdCharacter, Count:=1000
    Selection.MoveLeft Unit:=wdWord, Count:=2, Extend:=wdExtend
    Selection.Range.HighlightColorIndex = wdBrightGreen
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    End Sub

    I want Word to count 1000 characters "without spaces", highlight the two last words and wait for my action. However, it makes the count "with spaces" and as a newbie, I need your help

    Thanks in advance

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Character count with macro (Word 2003)

    Welcome to Woody's Lounge!

    As far as I know there is no built-in way to move a specified number of characters excluding spaces. Instead of

    Selection.MoveRight Unit:=wdCharacter, Count:=1000

    you could use

    Dim intCount As Integer
    Do
    Selection.MoveRight
    If Not Asc(Selection) = 32 Or Asc(Selection) = 160 Then
    intCount = intCount + 1
    End If
    Loop Until intCount = 1000

    (Character 32 is a space, character 160 is a non-breaking space). You may want to turn off screen updating temporarily before entering the loop with

    Application.ScreenUpdating = False

  3. #3
    New Lounger
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Character count with macro (Word 2003)

    Thanks for your time Hans.

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Character count with macro (Word 2003)

    Hi werther,

    How about:

    Sub Demo()
    Application.ScreenUpdating = False
    With Selection
    .Collapse
    .MoveRight Unit:=wdCharacter, Count:=1000, Extend:=wdExtend
    Do While .Range.ComputeStatistics(wdStatisticCharacters) < 1000
    .MoveRight Unit:=wdCharacter, Extend:=wdExtend, _
    Count:=1000 - .Range.ComputeStatistics(wdStatisticCharacters)
    Loop
    .Range.Words.Last.Select
    .Collapse (wdCollapseEnd)
    .MoveLeft Unit:=wdWord, Count:=2, Extend:=wdExtend
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •