Results 1 to 5 of 5

Thread: [RESOLVED] How to Close Excel without saving changes?

  1. #1

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Resolved [RESOLVED] How to Close Excel without saving changes?

    Very simple as I think

    in my project i'm automating a sheet on Excel in the background where Excel is not visible.
    I just want after printing that sheet to just close it without saving changes.

    and this is my code:

    Code:
    Dim xlApp As New Excel.Application
            Dim xlWorkBook As Excel.Workbook
            Dim xlWorkSheet As Excel.Worksheet
    
    
    'Here I need to add the code to avoid saving changes
            xlApp.Quit()
            Me.Close()

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: How to Close Excel without saving changes?

    Make sure you close all of the object appropriately, but in terms of the Workbook (which is the thing that gets saved), try this:
    Code:
    xlWorkBook.Close (SaveChanges:= False)

  3. #3

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Close Excel without saving changes?

    Quote Originally Posted by si_the_geek View Post
    Make sure you close all of the object appropriately, but in terms of the Workbook (which is the thing that gets saved), try this:
    Code:
    xlWorkBook.Close (SaveChanges:= False)
    Thank you very much
    It worked properly and closing the worksheet without saving.

    but the thing you were exactly expecting just happened
    It's not closing properly and Excel still showing in the Task Manger

    Although I was thinking this line of code is enough :
    Code:
    xlApp.Quit()

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: How to Close Excel without saving changes?

    Unfortunately it isn't, because Excel is a COM object you need to release it in a more complex way.

    I don't have example code handy, but there are several examples on this site, I think this one may be enough for you:
    http://www.vbforums.com/showthread.p...n-Task-Manager

  5. #5

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Close Excel without saving changes?

    Quote Originally Posted by si_the_geek View Post
    Unfortunately it isn't, because Excel is a COM object you need to release it in a more complex way.

    I don't have example code handy, but there are several examples on this site, I think this one may be enough for you:
    http://www.vbforums.com/showthread.p...n-Task-Manager
    I checked the site and it was so helpful

    What I End up with is just a simple code not the whole complicated process because the end user might need to use Excel again so I made it like this:

    from Inside the program after printing for example
    Code:
            xlWorkBook.Close(SaveChanges:=False)
            xlApp.Quit()
            xlApp = Nothing

    and when closing the form:

    Code:
            If Not xlApp Is Nothing Then
                xlApp = Nothing
            End If
            Me.Close()
    And my problem solved Thank you very very much si_the_geek

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width