Working with all files in directory

S

singeredel

I am trying to open all files in a directory and then run a third-party line
count program for which I don't have access to the programming. So far I have
come up with the following code, but there are a few problems:

Sub HannaniLineCount()
Dim pFileName As String
Dim pDoc As Word.Document
Dim pFileDir As String

pFileDir = CurDir (right now this is temporary until the code works)
pFileName = Dir(pFileDir + "\*.doc")
Do Until Len(pFileName) = 0
Set pDoc = Documents.Open(pFileName)
If Left(pDoc, 12) = "HannaniNOTES" Then
GoTo NextLoop
End If
PrepareWCLineCount
pDoc.Close SaveChanges:=False
pFileName = Dir
NextLoop:
Loop
End Sub

Sub PrepareWCLineCount()
DeleteDisclosure (another sub that works fine)
ActiveWindow.View.Type = wdNormalView
Application.Run MacroName:="Abacus.Counter.AbacusCountLines"
End Sub

1. The code does not return to the calling program after calling and
executing the macro to the line count program in the sub "PrepareWCLineCount".

2. The line count program requires some keystrokes to make it work and I
don't know how to actually program in keystrokes (these would be alt+C,
alt+S, and alt+X).

3. The code does not seem to cycle to the next file in the directory.

Any help would be appreciated. I am not a programmer, so I am sure there are
things I have missed in the code. Thanks!
 
J

Jezebel

Couple of problems --

1. Your loop logic is screwy. In your version, you're leaving the file open
if it passes the HannaniNotes test. (And there's NEVER any need to use Goto,
except for error handling.)

2. Dir returns only the filename, not the path, so you need to prepend the
path name in the Documents.Open() statement.

3. Use SendKeys to send keystrokes -- SendKeys "%S%C%X" You normally
need to send these BEFORE you call the app that uses them, but you'll need
to experiment with this, and also whether you need the Wait argument.

4. The problem with the code not returning from PrepareWCLineCount is not
something you can resolve unless it's to do with the keystrokes you need to
send. What does PrepareWCLineCount actually do, anyway?




Sub HannaniLineCount()
Dim pFileName As String
Dim pDoc As Word.Document
Dim pFileDir As String

pFileDir = CurDir (right now this is temporary until the code works) &
"\"

pFileName = Dir(pFileDir + "*.doc")
Do Until Len(pFileName) = 0

Set pDoc = Documents.Open(pFileDir & pFileName)

If Left$(pDoc, 12) <> "HannaniNOTES" Then
PrepareWCLineCount
End If

pDoc.Close SaveChanges:=False
pFileName = Dir
Loop

End Sub

Sub PrepareWCLineCount()
DeleteDisclosure (another sub that works fine)
ActiveWindow.View.Type = wdNormalView
Application.Run MacroName:="Abacus.Counter.AbacusCountLines"
End Sub
 
C

Charles Kenyon

S

singeredel

Thanks for your help...

In answer to you question about what PrepareWCLineCount actually does: It
searches the document for a long section of disclosure information that
appears in a certain type of report and deletes the disclosure info before
the line count starts so that the disclosure info is not included in the line
count. (The document is NOT saved so that the disclosure info remains in the
document).

Are you saying that there is no way to return to the calling program after
this line count macro runs?
 
J

Jezebel

I'm not saying there's no way. If it's well-behaved and functioning
correctly it will return on completion; but if not, there's nothing your
calling routine can do about it.
 
S

singeredel

This was a standalone program with its own dialog box, so I guess trying to
incorporate it into my code is not going to work?
 
J

Jezebel

Well not if you can't solve the problem of it not returning on completion
:) At the point where it's finished but not returning, what is actually
going on?
 
S

singeredel

Basically, the way the line count (standalone program) works on its own is it
works on the open file in Word. With the file open, when you activate the
program it brings up a dialogue box. You use the Alt+C command to count the
lines of the open document (based on options and settings established
relative to rate and line counting method and other options which can be
stored in separate .ini files), the Alt+S command to save the information to
a log file, and Alt+X to exit the program, whereupon the dialog box goes
away. At this point I have to manually activate this program for each file in
the directory after opening the file.
 
S

singeredel

Oh, you wanted to know what it does on completion: After the dialog box goes
away, I close the file without saving it and repeat the process on the next
file. There is no other action in my code other than to repeat the process:
open file, search for disclaimer and delete if there, execute line count,
close file without saving.
 

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