Timing problem?

E

Ebbe

Hey

I have written a procedure, that searches all *.doc's in a specific path,
prints them out and then deletes them.
The following sub is working perfectly when I run it in the debugger.
But "in real life" it prints out the first found document and then hangs up.
I suppose it is because the "Kill" is executed too quickly after the
"Close"?? I discovered some times that document is deleted, but the
related ~-workfile is still "alive".

Can I insert a sort of time delay between the Close and Kill?
Or is there an other way to solve the problem?

Ebbe

'-------------------------- begin
Sub AutoOpen()
Dim Pattern As String
Dim Path As String
Dim DocName As String

Path = ActiveDocument.BuiltInDocumentProperties("Keywords").Value
Pattern = Path & "\" & "*.doc"
DocName = Dir(Pattern)

Do While DocName <> ""
DocName = Path & "\" & DocName
Documents.Open FileName:=DocName, ReadOnly:=True

Application.PrintOut _
FileName:="", _
Range:=wdPrintAllDocument, _
Item:=wdPrintDocumentContent, _
Copies:=1, _
Pages:="", _
PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, _
Collate:=True, _
Background:=False, _
PrintToFile:=False, _
PrintZoomColumn:=0, _
PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0

ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

Kill DocName
DocName = Dir(Pattern)
Loop

Application.Quit
End Sub

'-------------------------- end
 
J

Jonathan West

Hi Ebbe,

You can use the Sleep API function.

Add the following above the first routine in your module.

Private Declare Sub Sleep Lib "kernel32" Alias _
"Sleep" (ByVal dwMilliseconds As Long)

Then, whenever you want to add a delay, just use a line of code like this

Sleep 50 ' adds a 50ms delay

You could put an error handler round the Kill command, like this

On Error Goto Errhandle:
Kill DocName
On Error Goto 0

and then at the end of thr routine, add the following

Exit Sub ' to stop the error handler being executed by normal code
Errhandle:
Sleep 50
Err.Clear
Resume

What this does, is wait 50 ms, clear the error, and try again. Rinse &
repeat until successful. You might want a counter in there so that if it
fails after 10 tries or so, you go on to the next one.

A further alternative is to print all the files and then delete them in two
separate loops, like this

Sub AutoOpen()
Dim Pattern As String
Dim Path As String
Dim DocName As String

Path = ActiveDocument.BuiltInDocumentProperties("Keywords").Value
Pattern = Path & "\" & "*.doc"

DocName = Dir(Pattern)
Do While DocName <> ""
DocName = Path & "\" & DocName
Documents.Open FileName:=DocName, ReadOnly:=True

Application.PrintOut _
FileName:="", _
Range:=wdPrintAllDocument, _
Item:=wdPrintDocumentContent, _
Copies:=1, _
Pages:="", _
PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, _
Collate:=True, _
Background:=False, _
PrintToFile:=False, _
PrintZoomColumn:=0, _
PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0

ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
DocName = Dir()
Loop

DocName = Dir(Pattern)
Do While DocName <> ""
DocName = Path & "\" & DocName
Kill DocName
DocName = Dir()
Loop
Application.Quit
End Sub
 
E

Ebbe

Hi Jonathan

It works!
A simple sleep between close and kill did the trick :)

Thanks! Ebbe
 

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