How to close Excel c# - c#

this is my code, I have seen others closing excel sheets this way but why does this not work. There are no errors in the code execution but the app still seems to be running in the background
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook Sheet = Excel.Workbooks.Open("C:\\Users\\Maxine\\Testing.xlsx");
Microsoft.Office.Interop.Excel.Worksheet x = ((Microsoft.Office.Interop.Excel.Worksheet)Excel.ActiveSheet);
Sheet.Close(false,Type.Missing,Type.Missing);
Excel.Quit();

You need to actually release the COM object. See here, but you need to do Marshal.FinalReleaseComObject on the Excel object.

Related

Releasing Excel*32 process after operation complete

Trying to figure out why my EXCEL*32 process remains in use until both the my application AND the excel file are closed. I must be missing something after creation, it's like the application is holding onto the EXCEL *32 resource after this code. Any suggestions to get it to 'let go' after it's export operation is completed?
Also, I do not want to close the newly created Excel sheet, I just want to release the resource being used in relation to my actual .net application.
Application xls = new Application();
xls.SheetsInNewWorkbook = 1;
// Create our new excel application and add our workbooks/worksheets
Workbook Workbook = xls.Workbooks.Add();
Worksheet CrossoverPartsWorksheet = xls.Worksheets[1];
// Create our new excel application and add our workbooks/worksheets
Workbook Workbook = xls.Workbooks.Add();
Worksheet CrossoverPartsWorksheet = xls.Worksheets[1];
/////////////////////////////////////////
// < DO EXCEL EXPORT OPERATIONS HERE > //
/////////////////////////////////////////
// Release our resources.
Marshal.ReleaseComObject(Workbook);
Marshal.ReleaseComObject(CrossoverPartsWorksheet);
Marshal.ReleaseComObject(xls);
When you write
Workbook Workbook = xls.Workbooks.Add();
CLR creates RCW (Runtime Callable Wrapper) objects not only for Workbook, but for Workbooks collection too(coz you need object that then will be used for Add() method). And if CLR creates RCW object and you do not keep reference - you can't finalize it.
So, the main rule:
You should avoid double-dot-calling expressions:
var workbooks = xls.Workbooks;
var workbook = workbooks.Add();
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(workbooks);
Marshal.FinalReleaseComObject(xls) is what your looking for.

Pass parameters to VSTO-enabled Excel Spreadsheet on server

I created an Excel 2010 Workbook project with to customize some ribbon extensions. It uses a webservice to read in data to pre-populate the form. My question is, how can I pass in some parameters, for example a record ID, to the workbook when it is requested from the server?
I think my scenario is similar to this question, which was never answered: Pass Data into a VSTO Excel Workbook?
There is a way of passing data to a workbook which personally I don't really like, but maybe it can suit you. Basically, you set values for specific cells in the workbook, and then process those values in Excel's event handler:
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Open(filepath);
var sheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];
var range = sheet.Range["A1"];
range.Value2 = "some value";

Get the current Workbook Object in C#

I've been writing an application in C# which creates Custom Document properties in an Excel spreadsheet, I have a function for this which takes in a Workbook Object...
However, actually getting the current Workbook object is proving to be quite annoying, I am using ExcelDNA to add functionality, however, I can't seem to pass my function a valid Workbook COM object.
If you need to find the activeworkbook with C#, if you are using Office Interop, you can try this kind of code:
(Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
[Source]
This is the way I am currently doing it it seems to work really well
using Excel = Microsoft.Office.Interop.Excel;
Then you get active workbook
//Gets Excel and gets Activeworkbook and worksheet
Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;
oXL = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
oXL.Visible = true;
oWB = (Excel.Workbook)oXL.ActiveWorkbook;
docProps = oWB.CustomDocumentProperties
Then I would try what you have and see how it works
Hope this helps
As #Govert explained above in his comment:
using Excel = Microsoft.Office.Interop.Excel;
using ExcelDna.Integration;
// Get the correct application instance
Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
// Get active workbook
Excel.Workbook wbook = xlapp.ActiveWorkbook;
GetActiveObject() looks in the Running Object Table (ROT) and gives you the last Excel instance opened which may not corresponding with top Z order excel window.
Loop through the Z order and find the matching workbook.
See this link: -
https://social.msdn.microsoft.com/Forums/office/en-US/060000d8-a899-49bf-a965-0576dee958d4/how-to-get-active-application?forum=exceldev

How to "link" to an already open Excel.Application with Interop c#

I can create an instance of Excel using:
Microsoft.Office.Interop.Excel.Application gXlApp = new Microsoft.Office.Interop.Excel.Application();
gXlWb= gXlApp.Workbooks.Add(Missing.Value);
gXlApp.Visible = true;
gXlApp.DisplayAlerts = false;
BUT is there a way to "point" gXlApp at an instance of Excel that is already open?
Currently I have to start Excel from c# then open the workbook and run my code.

Set Margins In Excel Using Excel Interop VB.Net

Anyone have code to set margins(top,left,right,bottom) using excel interop and vb.net. I think it must be part of the worksheet object but maybe the workbook object. Having a tough time finding an example. Thanks in advance.
I found it its part of the worksheet object...
i.e.
xlWorkSheet.PageSetup.TopMargin=0.5
Margins are set through the PageSetup object which you get from on the WorkSheet.PageSetup property.
Margin values have to mentioned in points. Use InchesToPoints(Double) or CentimetersToPoints(Double) to specify the value.
Eg:
Microsoft.Office.Interop.Excel.Application _ExcelAppl = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook _ExcelWorkBook = oXL.Workbooks.Add(missing);
Microsoft.Office.Interop.Excel.Worksheet _ExcelWorkSheet = oWB.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
_ExcelWorkSheet.PageSetup.TopMargin = _ExcelAppl.InchesToPoints(0.25);

Categories