How to use getActiveObject ("Excel.Application")

I need to finish the application in C #.

Now I want to get the function that controls the Excel file to get the data.

I used getActiveObject("Excel.Application"), but it returns nothing. I can not use Excel.Applicationin VS2008, and is used instead Microsoft.Office.Interop.Excel.Application.

So is there any other way to do this?

Microsoft.Office.Interop.Excel.Application e = (Microsoft.Office.Interop.Excel.Application)Marshal.GetActiveObject("Excel.Application");
+5
source share
5 answers

After about half a day of the game, I finally understood how to make this work so that you can click on an open copy of Excel. My users have complained that too many instances of Excel are open.

Here is a snippet of what I did to make it work:

_Application excelApp;

try
{
  excelApp = (_Application)Marshal.GetActiveObject("Excel.Application");
}
catch(Exception)
{
  // this is important. If Excel is not running, GetActiveObject will throw
  // an exception
  excelApp = null;
}

if( excelApp == null )
{
  excelApp = new ApplicationClass();
}

, , , .

+3

Excel "Excel", using ( ) :

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application Microsoft.Office.Interop.Excel.Application.

, Marshal.GetActiveObject , . :

(1) , Excel? , Excel:

Excel.Application xlApp = new Excel.Application();

(2) Excel , , Excel (ROT), Excel . . Visual #.NET Office . , Marshal.GetActiveObject - null - .

, ...

Mike

+1

Excel,

using Excel=Microsoft.Interop.Office.Excel;

Microsoft.Interop.Office.Excel , :

using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using static MMCAPP2010.Profile;
using System.Runtime.InteropServices;

namespace MMCAPP2010
{
    public class ExcelTemplateLoad
    {
        public void DeserializeObject(string filename)
        {
            Excel.Application instance;
            Workbook wb = null;
            try
            {
                //getting the current running instance of an excel application
                instance = (Excel.Application)Marshal.GetActiveObject("Excel.Application");   
            }
            catch
            {
                instance = new Excel.Application();
            }

            //opening the template
            wb = instance.Workbooks.Open(@"C:\Users\U1152927\Downloads\sample.xltx");
+1

See MS KB as indicated in the link below ...

http://support.microsoft.com/kb/238610

0
source

This is what I was looking for - I got the impression that if the instance was open, I could extract a workbook from it and should not open it with the path to the file - thanks!

0
source

Source: https://habr.com/ru/post/1716194/


All Articles