Excel instance won't close

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fahadqureshi
    New Member
    • Oct 2007
    • 28

    Excel instance won't close

    I am running two visual basic modules in Access and keep coming across an annoying problem.
    Both the vb modules transfer an excel spreadsheet to an access database but for some reason after the module has executed the excel spread sheets remain open in memory.
    So if for some reason i try to go open the spreadsheets in excel i get a message saying that the spreadsheet is locked and can only be open in read-only mode.
    When i press Ctrl+Alt+Delete , open task manager, and go to processes i can see two EXCEL.EXE process's running.
    Once I end the process i can open the spread sheet and use it but otherwise it stays locked.

    I researched online and came across the following code:

    Code:
    myExcelApp.ActiveWorkbook.Close
    myExcelApp.Application.Quit
    I tried using this code and it solved half my problem. So now what its doing is closing one of the EXCEL.EXE processes but one still remains open.

    What kind of code can I use to close/exit all spreadsheets that might be open in memory.
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Originally posted by fahadqureshi
    I am running two visual basic modules in Access and keep coming across an annoying problem.
    Both the vb modules transfer an excel spreadsheet to an access database but for some reason after the module has executed the excel spread sheets remain open in memory.
    So if for some reason i try to go open the spreadsheets in excel i get a message saying that the spreadsheet is locked and can only be open in read-only mode.
    When i press Ctrl+Alt+Delete , open task manager, and go to processes i can see two EXCEL.EXE process's running.
    Once I end the process i can open the spread sheet and use it but otherwise it stays locked.

    I researched online and came across the following code:

    [CODE=VB]myExcelApp.Acti veWorkbook.Clos e
    myExcelApp.Appl ication.Quit[/CODE]

    I tried using this code and it solved half my problem. So now what its doing is closing one of the EXCEL.EXE processes but one still remains open.

    What kind of code can I use to close/exit all spreadsheets that might be open in memory.
    Hello Hello!

    It looks like you are missing:

    [CODE=VB]

    Set YourWorkSheet = Nothing
    Set YourWorkBook = Nothing
    Set YourApp = Nothing

    [/CODE]

    Something like that, I think you need to say that:-)

    Try it!

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Yeah, you need to close/release/whatever all of the objects you create.

      Comment

      • fahadqureshi
        New Member
        • Oct 2007
        • 28

        #4
        I tried that and it's closing only one of the EXCEL.EXE 's while one remains open.

        Also, is this the correct command or am I doing this incorrectly?

        Set myExcelAppWorks heets = Nothing
        Set myExcelAppWorkB ooks = Nothing
        Set myExcelApp = Nothing

        Instead of YourWorkSheet and YourWorkBook I would use myExcelApp.
        Last edited by Killer42; Dec 20 '07, 10:52 PM.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          As far as I know it looks alright. However, as a general rule aren't you normally supposed to Close each object before setting your reference to Nothing?

          (I don't work a lot with objects, so don't assume I know what I'm talking about.)

          Comment

          • Dököll
            Recognized Expert Top Contributor
            • Nov 2006
            • 2379

            #6
            Great point, I dunno objects:-)

            ...perhaps close is also needed. I would try them both; it looks like it is working. I'll check the code to see if anything's a miss there.

            Does it give your the same treatment though? I am not sure if you said that...

            In a bit!

            Comment

            • Dököll
              Recognized Expert Top Contributor
              • Nov 2006
              • 2379

              #7
              Originally posted by Dököll
              Great point, I dunno objects:-)

              ...perhaps close is also needed. I would try them both; it looks like it is working. I'll check the code to see if anything's a miss there.

              Does it give your the same treatment though? I am not sure if you said that...

              In a bit!
              Please fogive me if I seem standoffish with the code, I have not worked too much with excel. Try nonetheless, and I will also give it a whirl and see if anything pops up. I must admin please I am not sure what the resluts will be when you run it. Let's give it a go and see:-)

              [CODE=VB]

              With myWorkBook

              .Save

              .Close
              End With

              myApp.Quit
              Set myWorkSheet = Nothing
              Set myWorkBook = Nothing
              Set myApp = Nothing
              [/CODE]

              Thing is I messed it up so much that I do not what going on with it.
              I probably made a typo up there too:-)

              Dököll
              Last edited by Dököll; Dec 21 '07, 02:46 AM. Reason: anyhing:-)

              Comment

              • kadghar
                Recognized Expert Top Contributor
                • Apr 2007
                • 1302

                #8
                try making the Excel.Applicati on visible, then close it.

                It's not the "right" way, but it works well.

                Comment

                • silentv8
                  New Member
                  • Feb 2008
                  • 1

                  #9
                  TRUE FIX ... or rather ... THIS IS HOW IT SHOULD BE


                  dim appExcel as Excel.Applicati on <- this is variable declaration only
                  .
                  . declare whatever you need
                  .

                  Set appExcel = NEW Excel.Applicati on <--- this line declares appExcel as New Object to be run in a new instance rather than object to be run within MS ACCESS instance.
                  .
                  . set whatever you need (including worbook and worksheet)
                  .


                  Try eliminating SET statment of appExcel and run your program .. See task list
                  Try setting appExcel without NEW declaration and run your program ... See task list
                  Try setting appExcel with NEW declaration and run your program ... See task list

                  .
                  . do your stuff here
                  .

                  workbook.save
                  workbook.close

                  set worksheet=nothi ng
                  set workbook=nothin g
                  appExcel.quit
                  set appExcel=nothin g

                  It took me about 1 day to figure things out ( as I missed NEW declaration in the set statement... so I hope this would benefit someone somewhere ).

                  excel should close with above standard closing statements

                  if you run recursive function/procedure with NEW instance of appExcel ... you need to close each instance. I suggest you DECLARE and set appExcel OUTSIDE the RECURSIVE procedure/function so that you only have 1 instance of appExcel running and pass the object as a parameter (if possible-i havent tried it but i think it should work)...using standard VBA programming it will be hard to close each instance without an object identifier .

                  if you declare the appExcel outside any function or procedure (public declaration), you may have EXCEL lingering until you close MS ACCESS.

                  SilentV8

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by silentv8
                    TRUE FIX ...
                    Thanks for sharing. I'm sure it will be useful for others with similar problems.

                    Comment

                    Working...