Exit/Quit Excel without saving

Started by oradba4u, December 31, 2021, 12:11:05 PM

Previous topic - Next topic

oradba4u

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

This works for me:
Code (WINBATCH) Select


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

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

Quote from: oradba4u 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

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