Count lines after text - part 2

J

Jim Burke in Novi

The code you guys gave me to count lines in a document works perfectly,
except for the fact that it counts blank lines at the end of the document.
Sometimes the user must hit the enter key after entering the last name in the
list, and that gets counted. Any simple way to ignore blank lines at the end?
Here's the code I'm using:

Set myDoc = objWord.Documents.Add(dictationServerDir & fileName, , , False)
Set myRange = myDoc.Range
myRange.Find.Execute FindText:="cc:", MatchCase:=True

If myRange.Find.Found Then
myRange.SetRange myRange.End, myDoc.Range.End
SetCopyCount = myRange.Paragraphs.count
If SetCopyCount = 0 Or SetCopyCount > 6 Then
SetCopyCount = 1
End If
Else
SetCopyCount = 1
End If
 
J

Jean-Guy Marcil

Jim Burke in Novi said:
The code you guys gave me to count lines in a document works perfectly,
except for the fact that it counts blank lines at the end of the document.
Sometimes the user must hit the enter key after entering the last name in the
list, and that gets counted. Any simple way to ignore blank lines at the end?

Something like this perhaps?

Dim myDoc As Word.Document
Dim myRange As Word.Range
Dim SetCopyCount As Long
Dim i As Long

Set myDoc = objWord.Documents.Add(dictationServerDir & FileName, , , False)
Set myRange = myDoc.Range
SetCopyCount = 0

myRange.Find.Execute FindText:="cc:", MatchCase:=True
If myRange.Find.Found Then
With myRange
.SetRange myRange.End, myDoc.Range.End
For i = .Paragraphs.Count To 1 Step -1
If Trim(.Paragraphs(i).Range.Text) = Chr(13) Then
SetCopyCount = SetCopyCount + 1
End If
Next
SetCopyCount = .Paragraphs.Count - SetCopyCount
If SetCopyCount = 0 Or SetCopyCount > 6 Then
SetCopyCount = 1
End If
End With
Else
SetCopyCount = 1
End If


Note that I check for empty paragraphs and also for paragraphs that contain
nothing but spaces (That is done with the "Trim" bit...).
 

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