BE sure the workbook is closed

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

BE sure the workbook is closed

Post by sal21 »

Code: Select all

....
Set XLAPP = CreateObject("Excel.Application")
Set XLWB = XLAPP.Workbooks.Open(DIR_FILE & NOMEFILE)
 Set XLWS = XLWB.Worksheets("FOGLIO1")
            ....
wath is the correct code to close the workbook?

I use this but not sure...

Code: Select all

Sub CHIUDI_EXCEL()
XLWB.Close
    Set XLWB = Nothing
    Set XLWS = Nothing
    XLAPP.Application.Quit
    Set XLAPP = Nothing
    End sub
note:
all var are Public

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: BE sure the workbook is closed

Post by HansV »

It's best to specify explicitly whether the workbook should be saved or not:

Code: Select all

    XLWB.Close SaveChanges:=True
if you want it to be saved, or

Code: Select all

    XLWB.Close SaveChanges:=False
if you do NOT want it to be saved, or if you already saved it just before calling CHIUDI_EXCEL.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: BE sure the workbook is closed

Post by sal21 »

HansV wrote:
03 Mar 2021, 10:49
It's best to specify explicitly whether the workbook should be saved or not:

Code: Select all

    XLWB.Close SaveChanges:=True
if you want it to be saved, or

Code: Select all

    XLWB.Close SaveChanges:=False
if you do NOT want it to be saved, or if you already saved it just before calling CHIUDI_EXCEL.
:cheers: