Results 1 to 4 of 4
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    FileCloseAll (WordXP)

    I've got it working (I think), so this is more for reference than a plea for help.

    The code below doesn't work - it leaves a few documents hanging around. I suspect that the failure is somehow related to iteration through collections.<pre>Public Sub FileCloseAll()
    Call Initialise(strcApplication)
    MsgPopUp Date & " FileCloseAll"
    '''
    ''' The user has issued a Close request on each open document.
    ''' For each open document
    ''' Try to close the document.
    '''
    Debug.Print "start with " & Application.Documents.Count
    Dim doc As Document
    For Each doc In Application.Documents
    Debug.Print doc.Name
    If (doc.Name = MacroContainer.Name) And (doc.Path = MacroContainer.Path) Then
    MsgBox "skipping " & doc.FullName
    Else
    Call CloseOneDocument(doc)
    End If
    Next doc
    '''
    End Sub</pre>

    The code below does work - by accumulating all document (names) in a string array and processing that string array.<pre>Public Sub FileCloseAll()
    Call Initialise(strcApplication)
    MsgPopUp Date & " FileCloseAll"
    '''
    ''' The user has issued a Close request on each open document.
    ''' For each open document
    ''' Try to close the document.
    '''
    Debug.Print "start with " & Application.Documents.Count
    Dim doc As Document
    Dim strAR() As String
    ReDim strAR(0)
    For Each doc In Application.Documents
    Debug.Print doc.Name
    If (doc.Name = MacroContainer.Name) And (doc.Path = MacroContainer.Path) Then
    MsgBox "skipping " & doc.FullName
    Else
    ' Call CloseOneDocument(doc)
    strAR(UBound(strAR)) = doc.FullName
    ReDim Preserve strAR(UBound(strAR) + 1)
    End If
    Next doc
    Dim lng As Long
    For lng = LBound(strAR) To UBound(strAR) - 1
    Call CloseOneDocument(Documents(strAR(lng)))
    Next lng
    '''
    End Sub</pre>


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FileCloseAll (WordXP)

    Another way would be to loop backwards through the Documents collection:

    Dim doc As Document
    Dim i As Long
    For i = Documents.Count To 1 Step -1
    Set doc = Documents(i)
    Debug.Print doc.Name
    If (doc.Name = MacroContainer.Name) And (doc.Path = MacroContainer.Path) Then
    MsgBox "skipping " & doc.FullName
    Else
    Call CloseOneDocument(doc)
    End If
    Next i

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileCloseAll (WordXP)

    > loop backwards through the Documents collection

    Thanks, Hans.
    Neater, cleaner, and doesn't introduce a string array to confuse the reader.
    I like it!

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FileCloseAll (WordXP)

    This technique can often be used when you are removing items from a collection. Looping forwards and removing items pulls the rug from under the loop, since the upper bound of the loop is changed by removing items. Looping backwards avoids this problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •