Link to home
Start Free TrialLog in
Avatar of Sandra Smith
Sandra SmithFlag for United States of America

asked on

Close not visible Excel workbook in ACCESS2010

I have a process that if it is interrupted can leave the workbook open but not visible  So, if the user runs he process again, I get a a read only message, but there is nothing they can see to close.  I got the below from another question on EE, but how do I change it to only close the one specific Excel.  That is, I want to have the workbook's name in this procedure.  Also, I commented out the WScript.Sleep 2000 as (1) I don't know what it does and (2) the process did close workbooks without it.  But I want it to only close the one and then exit the procedure.  The name of the workbook to close is "UploadTemplate".

Sandra

Public Sub CloseExcel()
On Error Resume Next
Dim objOffice As Object
Dim objWindow As Object
Dim WBook As Object

While Err.Number = 0
    Set objOffice = GetObject(, "Excel.Application")
    objOffice.DisplayAlerts = False
    For Each objWindow In objOffice.Windows
        objWindow.Activate
        Set WBook = objOffice.ActiveWorkbook
        WBook.Saved = True
        WBook.Close
    Next
    objOffice.DisplayAlerts = True
    objOffice.Quit
    Set objOffice = Nothing
''    WScript.Sleep 2000
Wend
MsgBox "Done"


End Sub
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sandra Smith

ASKER

Yes, it was invisible.  Thanks for the suggestion, it was driving me crazy.  

Sandra