Solved

C# closing an instance of  Microsoft.Office.Interop.Excel.Application excelApp

Posted on 2013-02-01
5
1,277 Views
Last Modified: 2013-04-02
Have tried a few including excelApp.ActiveWorkbook.Close(0,0,0); but it does not always close down the version thats open any ideas please
It seems to add 4 then take of 3 so that one stays and the open instances of excel seems to grow

// FIND ALL THE DATA OFF THE SPEAD SHEET FOR THE DISECTION INTO THE ARRAY
        private void GetCSVData(String filePath)
        {
          //  String filePath = @"C:\aeltest\@3rdmill.csv";
            // EXTRACT THE FILE NAME FOR THE SITE
            int startstr = filePath.IndexOf("@");
            string FileNme = filePath.Substring(startstr, filePath.Length - startstr);

            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(filePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(1);

            for (int intI = 1; intI < 100; intI++)
            {
                Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[intI, 1];
                Microsoft.Office.Interop.Excel.Range range2 = (Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[intI, 2];
                Microsoft.Office.Interop.Excel.Range range3 = (Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[intI, 3];
                Microsoft.Office.Interop.Excel.Range range4 = (Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[intI, 4];
                Microsoft.Office.Interop.Excel.Range range5 = (Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[intI, 5];

                try
                {
                    string cellValue1 = range1.Value2.ToString();
                    string cellValue2 = range2.Value2.ToString();
                    string cellValue3 = range3.Value2.ToString();
                    string cellValue4 = range4.Value2.ToString();
                    string cellValue5 = range5.Value2.ToString();

                    String abc = cellValue1;
                    CSVData[intI, 1] = cellValue1;
           //         PullApartTheCSVString( cellValue1, cellValue2, cellValue3, cellValue4, cellValue5, intI);
                    savetoDB( FileNme, cellValue1, cellValue2, cellValue3, cellValue4, cellValue5, intI);

                    //        savetoDB( String FileName, int intI);

                }
                catch
                {
                  //  intI = 101;
                }
            }
*********************************************************
             excelApp.ActiveWorkbook.Close(0,0,0);
        }
0
Comment
Question by:sydneyguy
5 Comments
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 125 total points
ID: 38846182
Try writing this in the finally block
finally
{
excelApp.ActiveWorkbook.Close(0,0,0);
}

Open in new window



Or by using a using keyword for this

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 125 total points
ID: 38846251
you have indeed to close and even dispose all objects (using using suggested above is the best method for that.
0
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 125 total points
ID: 38846489
Dim worksheet As New Microsoft.Office.Interop.Excel.Worksheet
worksheet = CType(excel.ActiveSheet, Microsoft.Office.Interop.Excel.Worksheet)
CType(worksheet, Microsoft.Office.Interop.Excel._Worksheet).Activate()
    '        ' If wanting excel to shutdown...
CType(excel, Microsoft.Office.Interop.Excel._Application).Quit()
0
 
LVL 33

Accepted Solution

by:
Norie earned 125 total points
ID: 38846594
If you want to quit the Excel application wouldn't it be this:
excelApp.Quit();

Open in new window


This looks more like it's for closing a workbook.
excelApp.ActiveWorkbook.Close(0,0,0);

Open in new window

0
 

Author Closing Comment

by:sydneyguy
ID: 39042665
thanks for all your help got it figured out in the end thanks to all your help
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dynamic Vlookup Function Formula Help 4 17
+1 to an even value 4 15
TT Auto DashBoard 4 33
Macro Filter fix 3 9
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now