Solved

Close not visible Excel workbook in ACCESS2010

Posted on 2013-12-20
2
397 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39732564
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
ID: 39732688
Yes, it was invisible.  Thanks for the suggestion, it was driving me crazy.  

Sandra
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 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