detecting hard carriage return's on line counting macro 
Author Message
 detecting hard carriage return's on line counting macro

I'm trying to develope a macro that will count lines but skip blank lines.
The macro below was suggested to me by Martin, and works with one exception,
it doesn't allow for hard carriage returns (vbVerticalTab contant), input
with shift-enter.

If there are, say 4 vertical tabs and then a paragraph marker, that's 5
blank lines the macro wouldn't pick up (it wouldn't pick up the follow-up
paragraph marker as blank because it contains something other than just
itself, i.e. the hard carriage returns).

can someone tell me how I can loop through the paragraph counting hard
carriage returns on lines by themselves?  And then I would also need to
count the final paragraph marker, but only if it followed directly behind a
hard carriage return (thus representing an additional blank line).

----Martin's macro:

Hi Mike
You could count the 'empty' lines and subract them from teh total number
of lines ... like this:

'-----
Sub CountLines()
Dim para As Paragraph
Dim iCount As Long

iCount = 0

For Each para In ActiveDocument.Paragraphs
    If para.Range = vbCr Then
        iCount = iCount + 1
    End If
Next para

MsgBox Str(ActiveDocument.BuiltInDocumentProperties(wdPropertyLines) -
iCount)

End Sub



Thu, 29 Apr 2004 05:38:35 GMT  
 detecting hard carriage return's on line counting macro
Hi,

Mike,

Use the following:

    Dim para As Paragraph, iCount As Integer
    iCount = 0
    ActiveDocument.UndoClear
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        Do While .Execute(FindText:="^l^p", Wrap:=wdFindContinue,
Forward:=True, ReplaceWith:="") = True
            iCount = iCount + 1
        Loop
    End With
    For Each para In ActiveDocument.Paragraphs
        If para.Range = vbCr Then
            iCount = iCount + 1
            para.Range.Delete
        End If
    Next para
    MsgBox "There are " &
ActiveDocument.BuiltInDocumentProperties(wdPropertyLines) & " non-blank
lines in the document."
    ActiveDocument.Undo (iCount)

Please post any response to the newsgroups for the benefit of others who may
also be following the thread.

Hope this helps,
Doug Robbins - Word MVP

Quote:
> I'm trying to develope a macro that will count lines but skip blank lines.
> The macro below was suggested to me by Martin, and works with one
exception,
> it doesn't allow for hard carriage returns (vbVerticalTab contant), input
> with shift-enter.

> If there are, say 4 vertical tabs and then a paragraph marker, that's 5
> blank lines the macro wouldn't pick up (it wouldn't pick up the follow-up
> paragraph marker as blank because it contains something other than just
> itself, i.e. the hard carriage returns).

> can someone tell me how I can loop through the paragraph counting hard
> carriage returns on lines by themselves?  And then I would also need to
> count the final paragraph marker, but only if it followed directly behind
a
> hard carriage return (thus representing an additional blank line).

> ----Martin's macro:

> Hi Mike
> You could count the 'empty' lines and subract them from teh total number
> of lines ... like this:

> '-----
> Sub CountLines()
> Dim para As Paragraph
> Dim iCount As Long

> iCount = 0

> For Each para In ActiveDocument.Paragraphs
>     If para.Range = vbCr Then
>         iCount = iCount + 1
>     End If
> Next para

> MsgBox Str(ActiveDocument.BuiltInDocumentProperties(wdPropertyLines) -
> iCount)

> End Sub



Thu, 29 Apr 2004 08:26:28 GMT  
 detecting hard carriage return's on line counting macro
Excelent, it sure seems to work.  Now I need to decipher it and learn a few
things :)

Thanks!



Quote:
> Hi,

> Mike,

> Use the following:

>     Dim para As Paragraph, iCount As Integer
>     iCount = 0
>     ActiveDocument.UndoClear
>     Selection.Find.ClearFormatting
>     Selection.Find.Replacement.ClearFormatting
>     With Selection.Find
>         .Forward = True
>         .Wrap = wdFindContinue
>         .Format = False
>         .MatchCase = False
>         .MatchWholeWord = False
>         .MatchWildcards = False
>         .MatchSoundsLike = False
>         .MatchAllWordForms = False
>         Do While .Execute(FindText:="^l^p", Wrap:=wdFindContinue,
> Forward:=True, ReplaceWith:="") = True
>             iCount = iCount + 1
>         Loop
>     End With
>     For Each para In ActiveDocument.Paragraphs
>         If para.Range = vbCr Then
>             iCount = iCount + 1
>             para.Range.Delete
>         End If
>     Next para
>     MsgBox "There are " &
> ActiveDocument.BuiltInDocumentProperties(wdPropertyLines) & " non-blank
> lines in the document."
>     ActiveDocument.Undo (iCount)

> Please post any response to the newsgroups for the benefit of others who
may
> also be following the thread.

> Hope this helps,
> Doug Robbins - Word MVP


> > I'm trying to develope a macro that will count lines but skip blank
lines.
> > The macro below was suggested to me by Martin, and works with one
> exception,
> > it doesn't allow for hard carriage returns (vbVerticalTab contant),
input
> > with shift-enter.

> > If there are, say 4 vertical tabs and then a paragraph marker, that's 5
> > blank lines the macro wouldn't pick up (it wouldn't pick up the
follow-up
> > paragraph marker as blank because it contains something other than just
> > itself, i.e. the hard carriage returns).

> > can someone tell me how I can loop through the paragraph counting hard
> > carriage returns on lines by themselves?  And then I would also need to
> > count the final paragraph marker, but only if it followed directly
behind
> a
> > hard carriage return (thus representing an additional blank line).

> > ----Martin's macro:

> > Hi Mike
> > You could count the 'empty' lines and subract them from teh total number
> > of lines ... like this:

> > '-----
> > Sub CountLines()
> > Dim para As Paragraph
> > Dim iCount As Long

> > iCount = 0

> > For Each para In ActiveDocument.Paragraphs
> >     If para.Range = vbCr Then
> >         iCount = iCount + 1
> >     End If
> > Next para

> > MsgBox Str(ActiveDocument.BuiltInDocumentProperties(wdPropertyLines) -
> > iCount)

> > End Sub



Thu, 29 Apr 2004 15:43:03 GMT  
 detecting hard carriage return's on line counting macro
Thanks :)

I see what the macro does by running it, and knowing that helps me to figure
out why it works by looking at the code (I don't know VBA for Word at all,
but I know VBA for excel so i can puzzle through it).  Looks like I need to
look into how Selection.Find works...

I have a fair amount of work to do to polish this thing up.  For instance, I
was trying to reconcile my line count with Word's (I haven't been able find
any documentation on how they calculate their lines) and I figured out that
they count a page break as a line.  (The page break thing really throws
everything off as the blank line after is counted as blank either by the
macro).

I can go from here I think (just need to delve a little deeper), you've both
given me a great place to start from.

Thanks again!



Quote:
> Hi,

> Mike,

> Use the following:

>     Dim para As Paragraph, iCount As Integer
>     iCount = 0
>     ActiveDocument.UndoClear
>     Selection.Find.ClearFormatting
>     Selection.Find.Replacement.ClearFormatting
>     With Selection.Find
>         .Forward = True
>         .Wrap = wdFindContinue
>         .Format = False
>         .MatchCase = False
>         .MatchWholeWord = False
>         .MatchWildcards = False
>         .MatchSoundsLike = False
>         .MatchAllWordForms = False
>         Do While .Execute(FindText:="^l^p", Wrap:=wdFindContinue,
> Forward:=True, ReplaceWith:="") = True
>             iCount = iCount + 1
>         Loop
>     End With
>     For Each para In ActiveDocument.Paragraphs
>         If para.Range = vbCr Then
>             iCount = iCount + 1
>             para.Range.Delete
>         End If
>     Next para
>     MsgBox "There are " &
> ActiveDocument.BuiltInDocumentProperties(wdPropertyLines) & " non-blank
> lines in the document."
>     ActiveDocument.Undo (iCount)

> Please post any response to the newsgroups for the benefit of others who
may
> also be following the thread.

> Hope this helps,
> Doug Robbins - Word MVP


> > I'm trying to develope a macro that will count lines but skip blank
lines.
> > The macro below was suggested to me by Martin, and works with one
> exception,
> > it doesn't allow for hard carriage returns (vbVerticalTab contant),
input
> > with shift-enter.

> > If there are, say 4 vertical tabs and then a paragraph marker, that's 5
> > blank lines the macro wouldn't pick up (it wouldn't pick up the
follow-up
> > paragraph marker as blank because it contains something other than just
> > itself, i.e. the hard carriage returns).

> > can someone tell me how I can loop through the paragraph counting hard
> > carriage returns on lines by themselves?  And then I would also need to
> > count the final paragraph marker, but only if it followed directly
behind
> a
> > hard carriage return (thus representing an additional blank line).

> > ----Martin's macro:

> > Hi Mike
> > You could count the 'empty' lines and subract them from teh total number
> > of lines ... like this:

> > '-----
> > Sub CountLines()
> > Dim para As Paragraph
> > Dim iCount As Long

> > iCount = 0

> > For Each para In ActiveDocument.Paragraphs
> >     If para.Range = vbCr Then
> >         iCount = iCount + 1
> >     End If
> > Next para

> > MsgBox Str(ActiveDocument.BuiltInDocumentProperties(wdPropertyLines) -
> > iCount)

> > End Sub



Thu, 29 Apr 2004 16:15:07 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Can't detect carriage return in keypress event

2. HELP! Counting carriage returns in multiline text box

3. HELP Counting carriage returns in multiline text box

4. counting tabs & hard returns in a document

5. carriage return, line feed

6. Carriage Return and line feed

7. Remove trailing carriage return/line feed from string..

8. print line without carriage return

9. Carriage Returns/Line Feeds

10. Carriage Return & Line Feed Question

11. executing carriage return in multi-line textbox in VB4

12. carriage return/line feed in textbox - help

 

 
Powered by phpBB® Forum Software