Solved

Close not visible Excel workbook in ACCESS2010

Posted on 2013-12-20
2
390 Views
Last Modified: 2013-12-20
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
Comment
Question by:ssmith94015
2 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:ssmith94015
Comment Utility
Yes, it was invisible.  Thanks for the suggestion, it was driving me crazy.  

Sandra
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
This collection of functions covers all the normal rounding methods of just about any numeric value.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now