L
liam_jones
I'm very new to Python, well IronPython to precise, and have been
having problems when using Excel.
The problem I'm having is the closing of my Excel object. I'm able to
successfully quit the Excel Application that I create, but when I open
a Workbook in the Application I can't successfully Quit Excel (by this
I mean I can quit it, but the Excel process isn't getting killed and I
have to manually go this through Task Manager).
I've given a sample of code below to hopefully make things clearer.
I've then given all of the information I think might be useful (sorry
if I've gone over the top!).
import System
import clr
from System.Data import *
from System.Web import *
from System.Web.UI import *
from clr import *
clr.AddReference("Microsoft.Office.Interop.Word")
clr.AddReference("Microsoft.Office.Interop.Excel")
clr.AddReference("Microsoft.Office.Interop.PowerPoint")
clr.AddReference("Office")
from Microsoft.Office.Interop.Word import ApplicationClass as
WordApplication
from Microsoft.Office.Interop.Excel import ApplicationClass as
ExcelApplication
from Microsoft.Office.Interop.PowerPoint import ApplicationClass as
PowerPointApplication
from Microsoft.Office.Interop.Word import WdReplace
from Microsoft.Office.Interop.Excel import XlCellType
from Microsoft.Office.Interop.Excel import XlSearchDirection
from System.Type import Missing
from System import GC
missing = Missing
FileLocation = "C:\\test.xls"
ExcelApp = ExcelApplication()
workbook= None
workbook = ExcelApp.Workbooks.Open(FileLocation, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing)
workbook.Save()
workbook.Close(SaveChanges=0)
ExcelApp.Quit()
workbook = None
ExcelApp = None
GC.Collect()
GC.WaitForPendingFinalizers()
I've simpilised the code by taking all of my Workbook processing from
it and the problem is still occurring. As I said above if I don't
create the workbook, then ExcelApp closes as expected (there are no
stray processes).
I've read many articles and postings over the last few days regarding
this, but have had no luck with anything I've seen, examples of this
are now given.
I've tried adding the below code, but with no luck.
ExcelApp.ActiveWorkbook.Save()
ExcelApp.ActiveWorkbook.Close(SaveChanges=0)
ExcelApp.Workbooks.Close()
I've also tried the below (again with no luck).
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
And, I've also tried the following (again with no luck!).
del(workbook)
del(excelApp)
It may seem strange using the Garbage Collector (well I wouldn't have
thought about using it here), but it was something that I read about
using for code written in C#. I've tried the code in C# (what I
normally write in) and all works fine, the only real difference is that
I'm setting the objects to NULL in C# and None here in IronPython -
Does this make a difference, is there something else I should be
setting it to? The Workbook object HAS to be set to NULL in C#, it's
then picked up by the Garbage Collector and the task disappears from
the Task Manager.
I did think of killing the actual EXCEL.EXE process at the end of my
code, but there might be several versions of the containing application
running on the same box, so I can't kill all of the Excel processes.
As I said above, I'm sorry if I've gone over the top in my description.
Any ideas or pointers would be greatly appreciated as I'm now going
round in circles.
Thanks in advance.
Rgds
Liam
having problems when using Excel.
The problem I'm having is the closing of my Excel object. I'm able to
successfully quit the Excel Application that I create, but when I open
a Workbook in the Application I can't successfully Quit Excel (by this
I mean I can quit it, but the Excel process isn't getting killed and I
have to manually go this through Task Manager).
I've given a sample of code below to hopefully make things clearer.
I've then given all of the information I think might be useful (sorry
if I've gone over the top!).
import System
import clr
from System.Data import *
from System.Web import *
from System.Web.UI import *
from clr import *
clr.AddReference("Microsoft.Office.Interop.Word")
clr.AddReference("Microsoft.Office.Interop.Excel")
clr.AddReference("Microsoft.Office.Interop.PowerPoint")
clr.AddReference("Office")
from Microsoft.Office.Interop.Word import ApplicationClass as
WordApplication
from Microsoft.Office.Interop.Excel import ApplicationClass as
ExcelApplication
from Microsoft.Office.Interop.PowerPoint import ApplicationClass as
PowerPointApplication
from Microsoft.Office.Interop.Word import WdReplace
from Microsoft.Office.Interop.Excel import XlCellType
from Microsoft.Office.Interop.Excel import XlSearchDirection
from System.Type import Missing
from System import GC
missing = Missing
FileLocation = "C:\\test.xls"
ExcelApp = ExcelApplication()
workbook= None
workbook = ExcelApp.Workbooks.Open(FileLocation, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing)
workbook.Save()
workbook.Close(SaveChanges=0)
ExcelApp.Quit()
workbook = None
ExcelApp = None
GC.Collect()
GC.WaitForPendingFinalizers()
I've simpilised the code by taking all of my Workbook processing from
it and the problem is still occurring. As I said above if I don't
create the workbook, then ExcelApp closes as expected (there are no
stray processes).
I've read many articles and postings over the last few days regarding
this, but have had no luck with anything I've seen, examples of this
are now given.
I've tried adding the below code, but with no luck.
ExcelApp.ActiveWorkbook.Save()
ExcelApp.ActiveWorkbook.Close(SaveChanges=0)
ExcelApp.Workbooks.Close()
I've also tried the below (again with no luck).
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
And, I've also tried the following (again with no luck!).
del(workbook)
del(excelApp)
It may seem strange using the Garbage Collector (well I wouldn't have
thought about using it here), but it was something that I read about
using for code written in C#. I've tried the code in C# (what I
normally write in) and all works fine, the only real difference is that
I'm setting the objects to NULL in C# and None here in IronPython -
Does this make a difference, is there something else I should be
setting it to? The Workbook object HAS to be set to NULL in C#, it's
then picked up by the Garbage Collector and the task disappears from
the Task Manager.
I did think of killing the actual EXCEL.EXE process at the end of my
code, but there might be several versions of the containing application
running on the same box, so I can't kill all of the Excel processes.
As I said above, I'm sorry if I've gone over the top in my description.
Any ideas or pointers would be greatly appreciated as I'm now going
round in circles.
Thanks in advance.
Rgds
Liam