473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Close an excel app in C#

I want to close excel application from C#, this is the piece of code I
am trying
"System.Runtime.InteropServices.Marshal.ReleaseCom Object(excelApp);"
GC.Collect();
It ain't working ! Any ideas please !!

Also tried excelApp.Quit();

This is fine, but on viewing "Task List" the application tab shows
"Excel.exe" running. And if I run the program again the message
"Administrator is using the file, file will open in readonly mode" shows.

Any ideas ?
Nov 16 '05 #1
5 3559
What if you try to close explicitely the file ?

Patrice

--

"Richie" <an*******@discussions.com> a écrit dans le message de
news:uD**************@TK2MSFTNGP14.phx.gbl...
I want to close excel application from C#, this is the piece of code I
am trying
"System.Runtime.InteropServices.Marshal.ReleaseCom Object(excelApp);"
GC.Collect();
It ain't working ! Any ideas please !!

Also tried excelApp.Quit();

This is fine, but on viewing "Task List" the application tab shows
"Excel.exe" running. And if I run the program again the message
"Administrator is using the file, file will open in readonly mode" shows.

Any ideas ?

Nov 16 '05 #2
Hi Patrice,
Thanks for your time.
I am really new to .net, can you tell me explicitly how to do it through
code ?
Thanks again !

"Patrice" <no****@nowhere.com> wrote in message
news:ul**************@TK2MSFTNGP12.phx.gbl...
What if you try to close explicitely the file ?

Patrice

--

"Richie" <an*******@discussions.com> a écrit dans le message de
news:uD**************@TK2MSFTNGP14.phx.gbl...
I want to close excel application from C#, this is the piece of code I
am trying
"System.Runtime.InteropServices.Marshal.ReleaseCom Object(excelApp);"
GC.Collect();
It ain't working ! Any ideas please !!

Also tried excelApp.Quit();

This is fine, but on viewing "Task List" the application tab shows
"Excel.exe" running. And if I run the program again the message
"Administrator is using the file, file will open in readonly mode" shows.
Any ideas ?


Nov 16 '05 #3
Nothing to do with .NET.

I meant I suspect the problem could arise when you try to close the Excel
application without first closing explicitely the workbook file.
I would try to close the workbook using the Excel object model before
quitting using the Excel object.

At least you shouldn't have any more the "file is open" error. You'll have
still to check if Excel is still in memory or not...

For now I believe that Excel doesn't "closes" properly if you don't close
yourself the workbook you are working on (remember in interactive mode
you'll have a dialog box asking to save or not).
Patrice

--

"Richie" <an*******@discussions.com> a écrit dans le message de
news:O$**************@TK2MSFTNGP12.phx.gbl...
Hi Patrice,
Thanks for your time.
I am really new to .net, can you tell me explicitly how to do it through
code ?
Thanks again !

"Patrice" <no****@nowhere.com> wrote in message
news:ul**************@TK2MSFTNGP12.phx.gbl...
What if you try to close explicitely the file ?

Patrice

--

"Richie" <an*******@discussions.com> a écrit dans le message de
news:uD**************@TK2MSFTNGP14.phx.gbl...
I want to close excel application from C#, this is the piece of code I
am trying
"System.Runtime.InteropServices.Marshal.ReleaseCom Object(excelApp);"
GC.Collect();
It ain't working ! Any ideas please !!

Also tried excelApp.Quit();

This is fine, but on viewing "Task List" the application tab shows
"Excel.exe" running. And if I run the program again the message
"Administrator is using the file, file will open in readonly mode" shows.
Any ideas ?



Nov 16 '05 #4
> "System.Runtime.InteropServices.Marshal.ReleaseCom Object(excelApp);"
GC.Collect();
It ain't working ! Any ideas please !!


following code works for us, however it is written in VB.NET to simpify the
automation. thus, you will have to rewrite it using reflection to user it
under C#.

the trick is to close all open workbooks before you call quit on Excell
object.

Wiktor Zychla

If Not (oExcel Is Nothing) Then

For i = 1 To oExcel.workbooks.count

oExcel.workbooks(i).close(False)

Next

oExcel.quit()

End If

oExcel = Nothing

GC.Collect()

GC.WaitForPendingFinalizers()
Nov 16 '05 #5
You shouldn't have to call GC.Collect and GC.WaitForPendingFinalizers. Simply calling Marshal.ReleaseCOMObject should be enough and its far more lightweight than triggering a full gen2 garbage collection.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
GC.Collect()

GC.WaitForPendingFinalizers()
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: IanW | last post by:
I don't know if this is in the right place but.. I am writing a VB.Net Class Library which uses Excel to produce workbooks If I create an instance of Excel and then close it I can get .NET to...
2
by: Lorenzo Melato | last post by:
Hi everyone, I have a very strange problem. If I open a .NET application from EXCEL using SHELL function, when I close EXCEL, the EXCEL.EXE process remain active and I must close it by Task...
5
by: Richie | last post by:
I want to close excel application from C#, this is the piece of code I am trying "System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);" GC.Collect(); It ain't working ! Any ideas...
0
by: Stewart Allen | last post by:
Hi there, I've got some code to open up an Excel workbook but what I want is for the code to pause until the Excel workbook is closed. Sub EditWorkbook(strFile as String) Dim xlApp As...
1
by: Gary Cobden | last post by:
Hi I have a routine that uses VBA to open a hidden occurence of Excel, and do background computations. However, in the event that the routine terminates abnormally, I have not been able to...
3
by: David Berman | last post by:
Hi, I've written an application to do a while bunch of Excel automation. I open a file, scan through all the worksheets, extract data, and then I try to close Excel. However, I can see that I'm...
5
by: Wenke Ji | last post by:
Hi I open a Excel workbook using below API: Set ExcelServer = CreateObject("EXCEL.Application") Set TargetWorkbook = ExcelServer.Workbooks.Open (CurrentBook) Befor the programm exit , I use...
4
by: Powerguy | last post by:
Hi all, I've spent countless hours trying to get this silly EXCEL process to close once I have used it. If anyone can help it would really REALLy be appreciated! Below is my code- Dim t As...
10
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
1
by: popsoftheyear | last post by:
I'm trying to automate some very simple things in excel, but have run into a roadblock (using c++). To detect excel closing, I just monitor the Excel Process using WaitForSingleObject. In order to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.