473,487 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 ?
Jul 21 '05 #1
5 23151
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 ?

Jul 21 '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 ?


Jul 21 '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 ?



Jul 21 '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()
Jul 21 '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()
Jul 21 '05 #6

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

Similar topics

1
14952
by: Andrei Ryazanov | last post by:
Hello, All! i try the following text from mvps.org Sub sTestXL() Dim objXL As Object Dim strWhat As String, boolXL As Boolean Dim objActiveWkb As Object If fIsAppRunning("Excel") Then Set...
3
8358
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
3024
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...
8
38156
by: SteveS | last post by:
I'm attempting to close EXCEL from within my VB.NET application. Using the excel object library to write data to my spreadsheet is working fine but when I try to quit application object it does not...
7
16195
by: SteveS | last post by:
I'm attempting to close EXCEL from within my VB.NET application. Using the excel object library to write data to my spreadsheet is working fine but when I try to quit application object it does not...
1
2194
by: datttanand | last post by:
how to close excel work book in java script?
4
19715
by: Scott | last post by:
I'm trying to get access to open, refresh and then close an excel spreadsheet for me. Below is the code I'm using to open and close Excel, I just can't get it to refresh my data linked back to my...
32
7259
by: RobinAG | last post by:
Hi everyone, I open an excel document, do some manipulation to it, and then close it through VBA in Access and free the references to it. When it closes, however, the process doesn't end and I...
1
2724
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...
1
1964
by: paul138 | last post by:
both the Red X (top right), as well as right clicking the tab on the bottom and closing, or even by pressing ALT+F4?? I understand that there are a lot of threads on various sites with this...
0
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
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
7137
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
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7349
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...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.