Counting spaces

C

Chuck

Hi,

I need some assistance with determining the number of
space characters in a line. I am currently counting the
total number of characters ok but now need to somehow
identify the number of spaces (or tabs) before the first
significant character in a line. I count total characters
by using the ConvertToTable method on the entire document,
then iterate throught the rows checking the number of
characters in a line. Please see the following snippet of
code for my method. (Assume all variables and objects
correctly defined)

myDoc.select
Set mysel = .Selection
mysel.ConvertToTable Separator:=wdSeparateByParagraphs,
NumColumns:=1, Format:=wdTableFormatNone, etc...(more
parameters here)
linect = 1
Do while linect < totalLines 'calc'd earlier in module
Charcnt = 0
Charcnt = mysel.Rows(linect).Range.Characters.Count
(...some more conditional code here)
mysel.MoveDown(wdLine)
linecnt = linecnt + 1
Loop

This gives me total character count per line but what I
also need is to identify how many spaces (or tab)
characters precede the first significant character.
Thanks in advance for your assistance.
 
P

Peter Hewett

Hi Chuck

You can use this code:

Public Sub CountLines()
Dim rngWhiteSpace As Word.Range
Dim lngLeadingsSpaces As Long

Set rngWhiteSpace = Selection.Range
rngWhiteSpace.Collapse wdCollapseStart
lngLeadingsSpaces = rngWhiteSpace.MoveWhile(" " & vbTab, wdForward)
Debug.Print lngLeadingsSpaces
End Sub

Public Sub LineByLine()
Dim rngWhiteSpace As Word.Range
Dim boolDone As Boolean
Dim lngCharactersInDoc As Long
Dim lngLeadingsSpaces As Long

' Total character count for the document
lngCharactersInDoc = _
ActiveDocument.Content.ComputeStatistics(wdStatisticCharactersWithSpaces)

' Iterate the document a line at a time, count leading spaces on each line

' Start by selecting the first line
Selection.HomeKey wdStory
Selection.MoveEnd wdLine
Do
boolDone = EndOfDocument

' Set range to start of the current line
Set rngWhiteSpace = Selection.Range
rngWhiteSpace.Collapse wdCollapseStart

' Count consecutive number of spaces and tabs
lngLeadingsSpaces = rngWhiteSpace.MoveWhile(" " & vbTab, wdForward)
Debug.Print lngLeadingsSpaces

' Select next line
Selection.Move wdLine, 1
Selection.MoveEnd wdLine
Loop Until boolDone
End Sub
Private Function EndOfDocument() As Boolean
EndOfDocument = Selection.Type = wdSelectionNormal And Selection.End = _
ActiveDocument.Content.End
End Function

I've just stuck in a Debug statement where you can count the leading number of white space
characters in each line, or whatever it is you want to do.

HTH + Cheers - Peter
 
C

Chuck

Thanks Peter --- no problems now mate! g'day
-----Original Message-----
Hi Chuck

You can use this code:

Public Sub CountLines()
Dim rngWhiteSpace As Word.Range
Dim lngLeadingsSpaces As Long

Set rngWhiteSpace = Selection.Range
rngWhiteSpace.Collapse wdCollapseStart
lngLeadingsSpaces = rngWhiteSpace.MoveWhile(" " & vbTab, wdForward)
Debug.Print lngLeadingsSpaces
End Sub

Public Sub LineByLine()
Dim rngWhiteSpace As Word.Range
Dim boolDone As Boolean
Dim lngCharactersInDoc As Long
Dim lngLeadingsSpaces As Long

' Total character count for the document
lngCharactersInDoc = _
ActiveDocument.Content.ComputeStatistics (wdStatisticCharactersWithSpaces)

' Iterate the document a line at a time, count leading spaces on each line

' Start by selecting the first line
Selection.HomeKey wdStory
Selection.MoveEnd wdLine
Do
boolDone = EndOfDocument

' Set range to start of the current line
Set rngWhiteSpace = Selection.Range
rngWhiteSpace.Collapse wdCollapseStart

' Count consecutive number of spaces and tabs
lngLeadingsSpaces = rngWhiteSpace.MoveWhile(" " & vbTab, wdForward)
Debug.Print lngLeadingsSpaces

' Select next line
Selection.Move wdLine, 1
Selection.MoveEnd wdLine
Loop Until boolDone
End Sub
Private Function EndOfDocument() As Boolean
EndOfDocument = Selection.Type = wdSelectionNormal And Selection.End = _
ActiveDocument.Content.End
End Function

I've just stuck in a Debug statement where you can count
the leading number of white space
 

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