How to Loop through document and gather statistics?

A

Al

I am an Access VBA programmer and don't really understand Word VBA programming.

Basically I have MS Word files that I would like to loop through searching
for particular word strings like "Private Sub", trap that entire line, then
count the number of lines until an "End Sub" word string is encountered, then
populate a table or something with the trapped line concatenating the count.
Then search the next and so on through the entire document. Every "Private
Sub" has a corresponding "End Sub".

In sudocode it would look like this;

Loop until EOF
if rtrim(right(line,11)) = "Private Sub" then
Sub(0) = line
SubCount = 1
else if rtrim(right(line,7)) = "End Sub" then
SubCount = SubCount + 1
Call PrintLine (this routine prints Sub(0) & SubCount to a table)
else
SubCount = SubCount + 1
End If
Next


These are not actual word documents as you probably have guessed, but this
is a needed function.

Thanks for your replies!!
 
H

Helmut Weber

Hi Al,

just for counting you don't have to loop at all.

The split-function returns a zero beased array
the upperbound of which you get with the ubound-function.

MsgBox UBound(Split(ActiveDocument.Range.Text, "End Sub")) - 1
MsgBox UBound(Split(ActiveDocument.Range.Text, "Private Sub")) - 1

See help on "split" and its arguments.

Assuming you are analyzing text
which represents code, and furthermore,
that there is no commented out code like ' End Sub.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

Al

That's slick Helmut,

My goal is to return the name of the "Private Sub" (line which includes that
text, and the number of lines in that sub "End Sub". This does tell me how
many subs I have. That's why I have to step through the document and parse
text. What does an MS Word loop structure look like? Or is there one.

Thanks!!
 
H

Helmut Weber

Hi Al,

Sub Macro2a()
Dim sTmp As String ' text of first paragraph in rDcm
Dim lPrg As Long ' number of paragraphs in rDcm
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "Sub *End Sub"
.MatchWildcards = True
While .Execute
rDcm.Select ' for testing
sTmp = rDcm.Paragraphs(1).Range.Text
sTmp = Left(sTmp, Len(sTmp) - 1)
lPrg = rDcm.Paragraphs.Count
MsgBox sTmp & " (" & lPrg & ")"
Wend
End With
End Sub

Note that rDcm collapses to the found range
and expands again from the found range to
the end of the doc automatically.

Of course, this depends on the regularity of you code.

There are lots of ways for improvement.
You may search for chr(13) & "Sub "
(Which would not process the first paragraph in the doc,
if that first paragraph starts with "Sub") :-(
You may search for chr(13) & "Public Sub "
You may search for chr(13) & "Private Sub "
You may define the end of a sub as "End Sub" & chr(13) etc.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

Al

Helmut,

This is really slick. Thanks!!

Is there a way to look for both:
.text = "Sub *End Sub" and
.text = "Function *End Function"
within the same search?

I set up 2 searchs which works; however, I loose the sequence that the subs
and functions as they are found in the document.
 
H

Helmut Weber

Hi Al,
Is there a way to look for both:
.text = "Sub *End Sub" and
.text = "Function *End Function"
within the same search?

not in a straightforward way.
However, can all be done,
but then things get really complicated.
No way around two searches, IMHO,
and remembering which one was found where
and comparing the results.

Too convoluted for a speedy tip.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

Al

Thanks Helmut. I got the search working with 2 searches and it's doing what
I need it to do now.

Thanks!!
 
R

Russ

Al,
As you find each range that contains a sub or function, you could capture
the the range start number, also, to temporarily use in sorting a table by
the range start number.

......When something is found then
load rDcm.Start along with sTmp & " (" & lPrg & ")" into adjoining table
cells.

Then after all the subroutines and functions are found, the column
containing the range start numbers could be used to sort the table and then
deleted if desired.

A TOC (table of contents) or bookmarks can be used for quickly going to the
start of particular subroutine or function in the document. Or your text in
the cells containing the names could be changed into crossreferences or
hyperlinks to jump to the corresponding text in the document.
 

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