Counting lines via VBA

Hi,

I have a document that I use to temporary hold a list of information I collect from the Internet.

I have a Macro to place the information from the Clipboard to the Document.

After the Information is Pasted to the Document, I change the Font, but I would like to know the number of lines.

Here is a list of things I would like to experiment with if they are possible...

     1. Count the total number of lines which just have a "Carriage Return" and no text?
     2. Count the total number of lines in a document ie 'DOCPROPERTY Lines'?
     3. Count the number of lines which has text and are not just "Carriage Returns" ie 1 and 2?
     4. Count all the times a word is found within a Document ie 'GOLD'?

Thank you in advance,

Neil

Answer
Answer

Use:

Dim i As Long, j As Long
Dim apara As Paragraph
Dim aword As range
i = 0
For Each apara In ActiveDocument.Paragraphs
    If Len(apara.range) = 1 Then
        i = i + 1
    End If
Next apara
MsgBox "The document contains " & i & " empty paragraphs."
MsgBox "The document contains " & ActiveDocument.BuiltInDocumentProperties(wdPropertyLines) & " lines of text."
MsgBox "I am not sure what you mean by item 3."
i = 0
For j = 1 To ActiveDocument.range.Words.count
    If Trim(ActiveDocument.range.Words(j)) = "GOLD" Then
        i = i + 1
    End If
Next j
MsgBox "The word 'GOLD' appears in the document " & i & " times."

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated April 14, 2025 Views 4,104 Applies to: