Word - count number of lines after some text

J

Jim Burke in Novi

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:' 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
 
K

Ken Snell \(MVP\)

Jim -

You might want to post your question in a WORD newsgroup so that any issues
with the WORD VBA code can be answered by those experts.

I haven't automated WORD from within ACCESS, but I do see a few issues in
your code that may cause some reference problems:

1) You're using ActiveDocument in the reference for MyRange. When
automating an application from another application, use fully qualified
references (using objects that you've declared) -- for example:
Set myRange = objWord.Documents(Documents.Count).Content

or

Set myRange = objWord.Documents("filename").Content

2) You're using a Selection object that may not be known to ACCESS
(Selection.MoveDown wdLine, 1). Such objects work well when you're using VBA
within the same application, but not so well when you're automating that
application from another application.
 

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