Finding number of lines after some text

J

Jim Burke in Novi

In Access VBA code I am opening a word document and finding 'cc:' in it. I
have that working
fine. I need to count the number of lines including and after the line where
'cc:' is found. the cc: list is always at the very end, and each name goes on
a separate line. I can't quite figure out how to do this - this is my first
attempt at using word within Access, so I'm a newbie! Any help is
appreciated. Here's what I have to find 'cc:' (that part I've tested and I
know it's working OK) and what I was trying to do to
count the lines (it just keeps looping inside the while loop) :

Set objWord = New Word.Application
objWord.Documents.Add "filename"
Set myRange = objWord.ActiveDocument.Content
myRange.Find.Execute "Cc:", True, , , , , True
ccCount = 0
If myRange.Find.Found Then
While Not myRange.End
ccCount = ccCount + 1
Selection.MoveDown wdLine, 1
Wend
End If

objWord.ActiveDocument.Close
objWord.Quit
 
D

Doug Robbins - Word MVP

When cc is found, set a range variable (ccrnge) to the .Range of the
Selection and then change the .End of ccrnge to the .End of the .Range of
the Document and then use ccrng.Paragraphs.Count to get the number of
paragraphs in ccrng

Set objWord = New Word.Application
Set myDoc = objWord.Documents.Add "filename"
myDoc.Selection.Find.Execute "Cc:", True, , , , , True
ccCount = 0
If myRange.Find.Found Then
Set ccrnge = Selection.Range
ccrnge.End = myDoc.Range.End
ccCount = ccrnge.Paragraphs.Count
End If


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jim Burke in Novi

DOug,
Thanks for the response. When I try to use this I get a compile error on

Set myDoc = objWord.Documents.Add "filename"

It doesn't expect a file name at the end - it tells me it expects the end of
the line. Also, you have

myDoc.Selection.Find.Execute "Cc:", True, , , , , True
ccCount = 0
If myRange.Find.Found Then...

But myRange is never set to anything in your example. The find is done with
the myDoc.Selection. I did get it to execute OK with some changes, but I get
a ccCount value of 13 when it should only be 2. Here's the code I have that's
returing a value of 13:

Set objWord = New Word.Application
objWord.Documents.Add "fileName"
Set myRange = objWord.ActiveDocument.Content
myRange.Find.Execute "cc:", True, , , , , False
ccCount = 0
If myRange.Find.Found Then
Set ccRange = Selection.Range
ccRange.End = myRange.End
ccCount = ccRange.Paragraphs.count
End If

The contents of my test document are:

Gladys Smith April 3, 2008

CC: Metastatic small cell lung carcinoma

IMPRESSION:

RECOMMENDATIONS:

Other notes here

Dr. name here

cc: Dr. Bala Setty
Dr. Joe Blow

Note that there are two cc:'s, but the first one is in caps and I'm telling
it to match case (at least I think I am!)

Jim B
 
J

Jean-Guy Marcil

Jim Burke in Novi said:
DOug,
Thanks for the response. When I try to use this I get a compile error on

Set myDoc = objWord.Documents.Add "filename"

It doesn't expect a file name at the end - it tells me it expects the end of
the line. Also, you have

myDoc.Selection.Find.Execute "Cc:", True, , , , , True
ccCount = 0
If myRange.Find.Found Then...

But myRange is never set to anything in your example. The find is done with
the myDoc.Selection. I did get it to execute OK with some changes, but I get
a ccCount value of 13 when it should only be 2. Here's the code I have that's
returing a value of 13:

Set objWord = New Word.Application
objWord.Documents.Add "fileName"
Set myRange = objWord.ActiveDocument.Content
myRange.Find.Execute "cc:", True, , , , , False
ccCount = 0
If myRange.Find.Found Then
Set ccRange = Selection.Range
ccRange.End = myRange.End
ccCount = ccRange.Paragraphs.count
End If

Doug was showing you that you should not use ActiveDocument to set your
document object... He may have typed faster that he can read...

Also, it is better to use named arguments with such a method (.Excute, that
is). Depending on the Word version, the number of arguments may differ, and
you may not get the desired results.

Try this:

Set objWord = New Word.Application
'Becasue of the "=", (...) are needed
Set myDoc = objWord.Documents.Add("filename")

Set myRange = myDoc.Range

myRange.Find.Execute FindText:="cc:", MatchCase:=True

ccCount = 0

If myRange.Find.Found Then
myRange.SetRange myRange.End, myDoc.Range.End
ccCount = myRange.Paragraphs.Count
End If
 
J

Jim Burke in Novi

Doug, Thanks for the reply. There were a couple of things in your example
that were off a little but, but Jean-Guy got me straightened out. It now
works perfectly. I appreciate your help.

Jim B
 
J

Jim Burke in Novi

Works like a charm! Thanks much. I would normally take the time to sit down
and learn the Word object model, but I'm so swamped now I don't ahve the time
to do that. I tried to do it by trial and error, using VBA help and VBA's
fill-in-the-properties-and-methods functionality. Managed to get some of it
anyway. Thanks again, I appreciate the help.

Jim B
 

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