Word Takes Unacceptable Length of Time to Close

P

Peter McWha

Why does Microsoft Word take an unacceptably long length
of time to close after running the following code snippet
from Microsoft Word (as a Microsoft Word macro)?

Is there a more efficient way to access the documents which
.. does not expose (display) the document
.. avoids the Word closing issue

I've tried a number of workarounds unsuccessfully, but I
welcome any suggestions.

Please advise.

Sub MAIN()
Dim Document as Document
Dim temp00 As Long
Dim thedocs(145) As String
thedocs(0) = "c:\thedoc000.doc"
' Initialization of thedocs(1-144) removed for brevity
thedocs(145) = "c:\thedoc145.doc"
For temp00 = 0 to UBound(thedocs)
Set Document = GetObject(thedocs(temp00))
Set Document = Nothing
Next temp00
End Sub
 
J

Jonathan West

Hi Peter

I would change your macro as follows

Sub MAIN()
Dim oDocument as Document
Dim temp00 As Long
Dim thedocs(145) As String

For temp00 = 0 to 145
thedocs(temp00) = "c:\thedoc" & Format(temp00, "000") & ".doc"
Next temp00
For temp00 = 0 to UBound(thedocs)
Set oDocument = Documents.Open(thedocs(temp00))
oDocument.Close wdDoNotSaveChanges
Set oDocument = Nothing
Next temp00
End Sub

But I'm wondering why you are wanting to open each document just to close it
again.
 
P

Peter McWha

I've not yet tried your suggestion, but thank you.

Of course I don't actually open a document and then close
it. I removed all of the intervening code for simplicity
in attempting to demonstrate the issue.

Thank you again.
 
A

Alex Ivanov

You may
Dim wdApp As Word.Application
Set wdApp=new Word.Application
' and then in the loop
wdApp.Documents.Open(...)
'...
wdApp.Quit wdDoNotSaveChanges
set wdApp=Nothing

HTH
Alex.
 
Top