WinOle excel: terminate excel instances

170 views
Skip to first unread message

Zoran Sibinovic

unread,
Dec 23, 2022, 4:59:18 AM12/23/22
to Harbour Users
Hi to all,
I made an application that opens 10 excel files using winole. The first is the one where some data is written, the other 9 are files from where the data is taken from.
Now, I want to close the excel files from where I get the data one by one at the moment I finish to use it and then to proceed to the usage of the next file.

Using a variety of winole code lines I cannot remove/close any opened excel instances before the application ends. I mean, all the excel instances remain in the windows task manager and then closes at the application exit but not during the appication running.

The purpose is to release the excel instances that are not use anymore and speed up the application.     

Any suggestion how to process this?
 Thank you 

poopall

unread,
Dec 27, 2022, 5:24:21 AM12/27/22
to Harbour Users
Havnt tried but try and open all workbooks under a different excel object

Zoran Sibinovic

unread,
Dec 27, 2022, 5:51:44 AM12/27/22
to Harbour Users
Yes, that is the way I already use it, the app opens every excel file in owns excel instance in windows. That is the way I want to avoid.
In the meantime I solved this by trying this code:

m_oExcel  := win_oleCreateObject("Excel.Application")    
   
m_workbook := oExcel:Workbooks:Open( KP01 )  
m_sheet := oExcel:Sheets("m_sheet1")
...
m_workbook:saved := .t.
m_workbook:close()
...
m_workbook := oExcel:Workbooks:Open( KP02 )
m_sheet := oExcel:Sheets("m_sheet1")
...
m_workbook:saved := .t.
m_workbook:close()
...

so in this case I open one excel instance, read the DATA  from the first sheet, then close and open the first sheet in the second and close ... and again every singles excel file. 
Of course you can open some other sheet that is not must to be the same.
In the task manager then during the app lifetime exist only one excel instance.
The m_workbook:saved := .t. is needed because some excel files, for compatibility or else, can hung the app due the save request even if no changes are made. With this we say to excel that the file is already saved so it doesn't have to check it at the exit.

Thanks
Regards

poopall

unread,
Dec 27, 2022, 9:53:28 PM12/27/22
to Harbour Users
Sorry I cannot see where you created oExcel, is this the same as m_oExcel, if it is create anoother instance call is m_oExcela   := win_oleCreateObject("Excel.Application")    

Zoran Sibinovic

unread,
Dec 28, 2022, 3:40:42 AM12/28/22
to Harbour Users
My mistake in the copy/paste code, sorry, there is the correct code

m_oExcel  := win_oleCreateObject("Excel.Application")    
   
m_workbook :=  m_oExcel:Workbooks:Open( KP01 )  
m_sheet := m_oExcel:Sheets("m_sheet1")
...
m_workbook:saved := .t.
m_workbook:close()
...
m_workbook := m_oExcel:Workbooks:Open( KP02 )
m_sheet := m_oExcel:Sheets("m_sheet1")
...
m_workbook:saved := .t.
m_workbook:close()

Regards

poopall

unread,
Dec 28, 2022, 4:53:10 AM12/28/22
to Harbour Users
Try
m_oExcelKP01 := win_oleCreateObject("Excel.Application")    
m_workbook :=  m_oExcelKP01:Workbooks:Open( KP01 )  
m_sheet := m_oExcelKP01:Sheets("m_sheet1")
m_workbook:saved := .t.
m_workbook:close()
m_workbook := Nil
m_sheet := Nil
Release m_oExcelKP01

m_oExcelKP02 := win_oleCreateObject("Excel.Application")    
m_workbook :=  m_oExcelKP02:Workbooks:Open( KP02 )  
m_sheet := m_oExcelKP02:Sheets("m_sheet1")
m_workbook:saved := .t.
m_workbook:close()
m_workbook := Nil
m_sheet := Nil
Release m_oExcelKP02


I havnt tested , but see if that give you the result you need





...


m_workbook := m_oExcel:Workbooks:Open( KP02 )
m_sheet := m_oExcel:Sheets("m_sheet1")

Zoran Sibinovic

unread,
Dec 28, 2022, 5:46:45 AM12/28/22
to Harbour Users
Yes that is a similar way and can work just always opens and closes the excel app instance
Let think to not close that instance and just circulate the workbooks/files in one instance.
In my new I ommited

m_oExcelKP01 := win_oleCreateObject("Excel.Application")    
m_workbook :=  m_oExcelKP01:Workbooks:Open( KP01 )  
m_sheet := m_oExcelKP01:Sheets("m_sheet1")
m_workbook:saved := .t.
m_workbook:close()
m_workbook := Nil
m_sheet := Nil
Release m_oExcelKP01

m_oExcelKP02 := win_oleCreateObject("Excel.Application")    
m_workbook :=  m_oExcelKP02:Workbooks:Open( KP02 )  
m_sheet := m_oExcelKP02:Sheets("m_sheet1")
m_workbook:saved := .t.
m_workbook:close()
m_workbook := Nil
m_sheet := Nil
Release m_oExcelKP02

and keept just only one variable m_oExcel, not m_oExcelKP01 and m_oExcelKP02 (in your example)

the 

m_workbook := Nil
m_sheet := Nil
Release m_oExcelKP01

is ok if you want to completely close the excel instance from windows

but if you keep it, without closing m_oExcel := win_oleCreateObject("Excel.Application") , and with only
m_workbook :=  m_oExcel:Workbooks:Open( KP01 )  
m_sheet := m_oExcel:Sheets("m_sheet1")
m_workbook:saved := .t.
m_workbook:close()

you close only the workbook and then, with alternatively open/close, open/close ... the next workbooks, you keep all the way, in the memory, the same excel instance and you havent to open it again.
Just a manipulation with workbooks and not excel instances.

example:
we have KP01.xls, KP01.xls ...

m_oExcelKP01 := win_oleCreateObject("Excel.Application")   - opens the excel app instance in the windows memory
m_workbook :=  m_oExcel:Workbooks:Open( KP01 )  - load the excel workbook/file in the instance 
m_sheet := m_oExcel:Sheets("m_sheet1") - focus to first sheet (in my example I renamed the default name from sheet1 to m_sheet1

.... other code that manipulate the sheet data

m_workbook:saved := .t. - send the saved signal to the excel instance regarding the workbook status
m_workbook:close() - close the active excel workbook/file

now just

m_workbook :=  m_oExcel:Workbooks:Open( KP02 )  - load the next excel workbook/file  in the existing excel instance without close the excel app in windows
...
and so on
Reply all
Reply to author
Forward
0 new messages