Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2002 VBA - Closing Last Document Closes App

Status
Not open for further replies.

buddythunder

Programmer
Jun 12, 2003
1
GB
Hi all,

I'm testing our macros in preparation for upgrading from Office 97 to Office 2002/XP. I'm having some problems with Word quitting when the last document is closed. In the code below, when run with no documents initially open in Word 2002, the first activedocument.close runs fine and the macro continues, but the one in the for loop cause Word to close. Why should this be, and how can I stop it? I could add a document at the start and kill it at the end, but it's an ugly kludge, any thoughts on this one?

Thanks for your time :)
Dave

Code:
Option Explicit

Sub Main()

Dim aDoc As Document
Dim i As Integer
Dim StrPath As String

If Dialogs(wdDialogFileOpen).Show = 0 Then
    MsgBox "Routine cancelled - Table Of Contents to close", vbOKOnly + vbCritical
    End
End If
    
StrPath = ActiveDocument.Path
ActiveDocument.Close

With Application.FileSearch
    .NewSearch
    .LookIn = StrPath
    .FileType = msoFileTypeAllFiles
    .SearchSubFolders = False
    .FileName = "*.*"
    .Execute
End With

For i = 1 To Application.FileSearch.FoundFiles.Count
    
    Set aDoc = Documents.Open(Application.FileSearch.FoundFiles.Item(i))
    If aDoc.Revisions.Count > 0 Then
        MsgBox "You cannot run macros on files that have unnaccepted" & vbCrLf _
            & "changes. Please ensure you accept or reject all tracked " & vbCrLf _
            & "changes on all files.", vbOKOnly + vbCritical, "Tracked Changes Located"
        aDoc.Close
        Set aDoc = Nothing
        End
    End If
    aDoc.Close savechanges:=wdDoNotSaveChanges
    Set aDoc = Nothing
    
Next i#
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top