Trying to access excel COM object as admin user

0

The objective is to access the running Excel workbook in the machine. Intention is to close the respective workbook though this application which was opened by the user.

Achieved: I used the following code snippet to access the Excel COM object

        Excel.Application instance = null;            
        try
        {
            instance = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
        }
        catch (Exception ex)//Excel not open
        {
            wasFoundRunning = false;
        }

        if (wasFoundRunning)
        {
            foreach (Excel.Workbook ss in instance.Workbooks)
            {
                string s = ss.Name;                    
            }
        }

Issue: Above code works only if the excel sheet and the application opened by the same user in the machine. I have to run the application as administrator and it fails. Exception as follows and it means that it cannot find any excel sheet running.

Exception:  Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))

It fails if I run my application elevated and user opens the sheet.

Any ideas to fix this?

c#
excel
ms-office
comobject
asked on Stack Overflow Jun 1, 2017 by Gopichandar • edited Jun 1, 2017 by Gopichandar

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0