Results 1 to 4 of 4
  1. #1
    Dave_D's Avatar
    Dave_D is offline Advanced Beginner
    Windows XP Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    67

    Closing Excel file

    I have the following code which loops thru cells until the condition is true. When the condition is true, I branch out of the loop to close the Excel file. The err.number is 0 in all events, but the file does not be closed upon checking the task manager.



    Set oXl = CreateObject("Excel Application")
    oXl.Workbooks.Open(Myfile)

    Do Until x = " " or x_cnt = 100
    If oXl.ActiveCell.Offset(R, C).Value <> Empty Then
    x = oXl.ActiveCell.Offset(R, C).Value
    x_cnt = x_cnt + 1
    Else
    Goto Exit1
    End If
    ......
    Loop

    Exit1:
    ....
    Msgbox (err.number) is 0 entering these next statements
    oXl.Application.Quit
    Msgbox (err.number) still 0
    Set oXl = Nothing
    Msgbox (err.number) still 0

    I've even tried oXl.Close(Myfile) and when I leave this statement as a part of the code, oXl.ThisWorkBook.Saved = True, err.number = 91. I've tried False as well, but still get err 91. File is task manager remains open.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    oxl.ActiveWorkbook.Close False
    oxl.application.Quit

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    It might be because you are doing the "Quit" first. This is sequence of events that I use:


    Code:
      Dim xlapp As Excel.Application
      Dim xlBook As Excel.Workbook, xlSheet1 As Excel.Worksheet, xlSheet2 As Excel.Worksheet
      '
      '  other code here
      '
      Set xlapp = CreateObject("excel.application")
      Set xlBook = xlapp.Workbooks.Open(filename)
      '
      ' lots of code here
      '
      xlBook.Save
      xlBook.Close
      xlapp.Workbooks.Close
      xlapp.Quit
      Set xlapp = Nothing
    It has worked so far.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I use the same order as John_G to close/quit Excel using automation. But I have had problems with Excel actually quitting (as seen in task manager).
    My solution was to add "DoEvents" to the code.

    John's code to demonstrate my close/quit mods
    Code:
      Dim xlapp As Excel.Application
      Dim xlBook As Excel.Workbook, xlSheet1 As Excel.Worksheet, xlSheet2 As Excel.Worksheet
      '
      '  other code here
      '
      Set xlapp = CreateObject("excel.application")
      Set xlBook = xlapp.Workbooks.Open(filename)
      '
      ' lots of code here
      '
      xlBook.Save
      xlBook.Close
      xlapp.Workbooks.Close
      DoEvents
      xlapp.Quit
      DoEvents
      Set xlBook= Nothing
      Set xlapp = Nothing
    The "DoEvents" command adds a delay so the previous command has time to complete execution. This has worked for me....

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 05-25-2016, 12:43 PM
  2. Replies: 2
    Last Post: 04-01-2016, 10:38 AM
  3. Replies: 7
    Last Post: 08-07-2014, 06:07 PM
  4. I'm not closing off Excel properly I think?
    By stildawn in forum Modules
    Replies: 7
    Last Post: 05-01-2014, 05:12 PM
  5. Need help opening/closing Excel files
    By djrickel in forum Programming
    Replies: 1
    Last Post: 03-25-2014, 08:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums