-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcelInteropService.cs
More file actions
61 lines (46 loc) · 2.3 KB
/
ExcelInteropService.cs
File metadata and controls
61 lines (46 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
namespace WindowTracker
{
/// <summary>
/// https://stackoverflow.com/questions/770173/how-to-get-excel-instance-or-excel-instance-clsid-using-the-process-id
/// https://pastebin.com/F7gkrAST
///
/// Note, that in the original code, excel is started if not open, which is not necessary here
/// </summary>
public class ExcelInteropService
{
// When I tested it with the excel main window handle, I got "XLMAIN" as the
// excel class name from buf.ToString(), but the child windows are EXCEL7, not sure what this means
private const string EXCEL_CLASS_NAME = "EXCEL7";
private const uint DW_OBJECTID = 0xFFFFFFF0;
private static Guid rrid = new Guid("{00020400-0000-0000-C000-000000000046}");
public delegate bool EnumChildCallback(int hWnd, ref int lParam);
[DllImport("oleacc.dll")]
public static extern int AccessibleObjectFromWindow(int hWnd, uint dwObjectId, byte[] rrid, ref Microsoft.Office.Interop.Excel.Window ptr);
[DllImport("user32.dll")]
public static extern bool EnumChildWindows(int hWndparent, EnumChildCallback lpEnumFunc, ref int lParam);
[DllImport("user32.dll")]
public static extern int GetClassName(int hWnd, StringBuilder lpClassname, int mMaxCount);
private bool EnumChildFunc(int hWndChild, ref int lParam)
{
StringBuilder buf = new StringBuilder(128);
GetClassName(hWndChild, buf, 128);
if (buf.ToString() == EXCEL_CLASS_NAME) { lParam = hWndChild; return false; }
return true;
}
public Microsoft.Office.Interop.Excel.Application GetOpenExcelApplication(Process p)
{
Microsoft.Office.Interop.Excel.Window ptr = null;
int hWnd = 0;
int hWndParent = (int)p.MainWindowHandle;
if (hWndParent == 0) throw new Exception("Excel Main Window not found.");
EnumChildWindows(hWndParent, EnumChildFunc, ref hWnd);
if (hWnd == 0) throw new Exception("Child Window not found.");
int hr = AccessibleObjectFromWindow(hWnd, DW_OBJECTID, rrid.ToByteArray(), ref ptr);
return ptr.Application;
}
}
}