• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

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
0
ssmith94015
Asked:
ssmith94015
1 Solution
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I have a process that if it is interrupted can leave the workbook open but not visible
How does the process get interrupted? By that, I mean if you're opening a workbook that is not visible, then obviously you're doing this through automation, and if so, then you have control over this, and should be able to shut down your code gracefully if it fails.

In general, you do this:

dim xl As New Excel.Application
Dim wb As Excel.WorkBook
etc etc

On Error GoTo Err:

<code here>

Exit:
  On Error Resume Next
  Set wb = Nothing
  xl.Close
  Set xl = Nothing
  Exit Sub ' or Function
Err:
  <code here to handle error>
  Resume Exit
0
 
ssmith94015Author Commented:
Yes, it was invisible.  Thanks for the suggestion, it was driving me crazy.  

Sandra
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now