Author Topic: Exit/Quit Excel without saving  (Read 411 times)

oradba4u

  • Full Member
  • ***
  • Posts: 107
Exit/Quit Excel without saving
« on: December 31, 2021, 12:11:05 pm »
All:
Using 2021D
I want to be able to open a .xlsx spreadsheet (1 worksheet) for READ ONLY purposes, then exit Excel, and continue in my program. This code seems to work fine:

Excel = ObjectOpen("Excel.Application")
Excel.visible = @TRUE

; Append Directory to filename and OPEN the Excel file...
ExcelFileName = StrCat(DirScript(), MyFile)
Excel.Workbooks.Open(ExcelFileName)

AWS=Excel.Worksheets(1)
AWS.Activate

; Position cursor to cell A3... First initialize cursor location (row & column)...
RowNum=3
ColNum=1
TheCell=AWS.Cells(RowNum,ColNum)

After I loop thru a few rows & columns, I want to exit Excel (no save), quit the Excel program and continue with my program.. I can't seem to get that to work. I try:
Excel.quit
but I get an error: "3246 Ole Object: Object does not exist, or period used instead of comma"
How can I just exit w/o saving?

Tks in advance

stanl

  • Pundit
  • *****
  • Posts: 1618
Re: Exit/Quit Excel without saving
« Reply #1 on: January 01, 2022, 03:15:57 am »
This works for me:
Code: Winbatch

cXLS = dirscript():"wakecounty.xlsx"  ; substitute with your own excel workbook
If ! FileExist(cXLS) Then Terminate(@TRUE,"Cannot Continue, missing Excel File",cXLS)
oXL = CreateObject("Excel.Application")
If oXL == 0 Then Exit
oXL.Visible          = @TRUE  ; change this to @FALSE to run hidden
oXL.ScreenUpdating   = @TRUE  ; if running hidden, change this to @FALSE
oXL.UserControl      = @TRUE
oXL.DisplayAlerts    = @FALSE
oXL.WorkBooks.Open(::FileName="%cXLS%",ReadOnly=1)
oWS = oXL.ActiveWorkBook.WorkSheets(1)
oWS.Activate()


Pause("Quit Excel","Click OK")


oXL.ActiveWorkbook.Close()
oXL.Quit()
Exit
 

oradba4u

  • Full Member
  • ***
  • Posts: 107
Re: Exit/Quit Excel without saving
« Reply #2 on: January 01, 2022, 04:37:37 pm »
Oh. Now I see wht I was doing wrong. Thanks!

Guess I spoke too soon.... After my program runs, and I'm doing clean-up (closing Excel), Before I exit, Excel asks me if I want to "Save, Don't Save, Cancel."
How can I tell Excel to exit w/o saving?

Right now, here's what I have in my WIL script:

oXL.ActiveWorkbook.Close()
oXL.Quit
Exit ;Exit WinBatch

stanl

  • Pundit
  • *****
  • Posts: 1618
Re: Exit/Quit Excel without saving
« Reply #3 on: January 03, 2022, 12:27:12 pm »
Oh. Now I see wht I was doing wrong. Thanks!

Guess I spoke too soon.... After my program runs, and I'm doing clean-up (closing Excel), Before I exit, Excel asks me if I want to "Save, Don't Save, Cancel."
How can I tell Excel to exit w/o saving?

Right now, here's what I have in my WIL script:

oXL.ActiveWorkbook.Close()
oXL.Quit
Exit ;Exit WinBatch

Please review what I posted:  oXL.DisplayAlerts    = @FALSE should prevent the popup