1

I'm extremely new to Visual Basic (and to this site) and need a little advice on how to properly exit Excel when using automation. A little background on my issue: we have a large Excel workbook that my boss has created to do hundreds of relatively complex calculations. Currently users are just using the spreadsheet to input their data, but right now I'm working on a vb.net user interface to allow them to enter their data, and to also provide us with more flexibility and features to make their lives easier and get them out of the horror that is Microsoft Excel.

Upon opening my .net app, I'm running a command to open the workbook in the background (the visible property of the Excel object is set to false) so we can use it. At some point we will migrate these calculations into the stand alone app, but for now that is not a cost effective solution so we're going to use the spreadsheet that's already developed. I've run into a little issue upon closing Excel. I'm able to close Excel using the FormClosing event without a hitch. My problem is this: if I have my application running (which means my desired workbook is open in the background) and I open up another workbook via a shortcut on my desktop, when I go to close my application while that other workbook is open, it tries to close that 2nd workbook and the user is prompted with Excel's save changes dialog box. How do i get around this? Am I going to need to locate the process that is specific to my calculation workbook and close that rather than closing the Excel object I created?

Here's my code for my FormClosing event:

Private Sub Form8_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

 'close the workbook

xlWorkbook.Close(SaveChanges:=False)

'clean up

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook)

xlWorkbook = Nothing

'close the excel application

xlApp.Quit()

'clean up

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

xlApp = Nothing

End Sub

EDIT: I'm aware this issue is happening because of the way Excel opens it's files. Unless you go to the start menu and open a new instance of Excel, Excel will open your file in the application object that is already running. This leads me to believe this issue may be unavoidable, but would love some feedback even if you're just brainstorming

Any advice is greatly appreciated. Thanks, Eric

PS, thanks to whomever put my code in code tags!! I promise I will figure out how to do that myself ;)

7
  • When you open your file, when you say "in the background" do you mean that excel is visible (minimised) or is it hidden?
    – NickSlash
    Apr 16, 2013 at 13:36
  • I mean Excel is hidden...the visible property is set to false, but this problem occurs even when I have the Excel object set to visible, so that's probably not the issue Apr 16, 2013 at 13:39
  • Do you want your excel sheet to be read-only? (so you're just reading the formulas out, and any changes should be discarded at the end)
    – NickSlash
    Apr 16, 2013 at 13:42
  • The workbook will be set as read only yes, the idea is that our internal users won't even know they are using Excel because the .net application is all they will ever see. All I'm really doing is sending text to ranges in the workbook and then populating textboxes on my form with values from the workbook Apr 16, 2013 at 13:44
  • Nothing will be saved inside the workbook, I am saving user inputs in separate files, when we get our mySQL db up and running I will be saving their inputs in their so they can recall and make corrections if needed, but that's months away Apr 16, 2013 at 13:53

1 Answer 1

0

You could add some code to the Workbook_BeforeClose to prevent the book being closed when its needed.

You can do this a number of ways, the simplest would be to add it directly into your worksheet, you might also be able to hook the event in vb.net (public withevents type thing, ive only done it in VBA so couldnt help with the .net variation)

Here's a simple VBA solution that might work.

Module - Give it a name eg: Main

Public Sub UnlockWorkbook()
Dim Window As Window

For Each Window In ThisWorkbook.Windows
    Window.Visible = True
Next Window

End Sub

Public Sub LockWorkbook()
Dim Window As Window

For Each Window In ThisWorkbook.Windows
    Window.Visible = False
Next Window

End Sub

ThisWorkbook Module

Private Sub Workbook_BeforeClose(Cancel As Boolean)

For Each Window In ThisWorkbook.Windows
    If Window.Visible = False Then
        Cancel = True
        If Application.Workbooks.Count = 1 Then
            Application.Visible = False
        End If
        Exit For
    End If
Next Window

End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
SaveAsUI = False
End Sub

When you load the workbook, using your application, call the "LockWorkbook" macro which will hide the workbook, and you can also hide the application too.

Now if a user opens another workbook, unhiding the application, when they close their workbook and try to close the application it should stop that action, and re-hide the application for you.

When you do want to close the workbook, you need to call the "UnlockWorkbook" macro which will allow the "Worksheet_BeforeClose" to complete without setting Cancel to true.

5
  • I've added the VBA to my workbook. The LockWorkbook macro is giving me some flack, but the UnlockWorkbook macro is preventing the other workbook from trying to close when I exit my .net application...which is exactly what I'm trying to solve. I think I can work this out from here. Thanks a million NickSlash!!!!! Apr 16, 2013 at 14:29
  • errr...I guess technically the Workbook_BeforeClose event is what's preventing the second workbook from trying to close...but whatever :) Apr 16, 2013 at 16:51
  • I didnt test the code extensively, there might be an issue with If Application.Workbooks.Count = 1 Then as there will probably be 2 workbooks open as the event is BEFORE its closed.
    – NickSlash
    Apr 16, 2013 at 18:55
  • I just want to add for anyone searching the web for this sort of issue, adding xlApp.IgnoreRemoteRequests = True to your load event prevents Excel from hi-jacking your application object when opening another workbook via your desktop, my document folder etc. This will help keep the workbook your .net app is using hidden if a user needs to open another workbook on their machine. Apr 17, 2013 at 13:55
  • Just don't forget to set IgnoreRemoteRequests to False at some point or you won't be able to open any workbooks Apr 17, 2013 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.