Пример #1
0
 private void Application_WindowActivate(
     Microsoft.Office.Interop.Excel.Workbook Wb,
     Microsoft.Office.Interop.Excel.Window Wn)
 {
     Logger.Info("Application_WindowActivate");
     AttachToCurrentWindow();
 }
Пример #2
0
        private static ExcelApplication InnerFromHandle(Int32 handle)
        {
            xlWin win = null;
            Int32 hr  = AccessibleObjectFromWindow(handle, DW_OBJECTID, rrid.ToByteArray(), ref win);

            return(new ExcelApplication(win.Application));
        }
Пример #3
0
        private static xlApp InnerFromHandle(Int32 handle)
        {
            //assign the handle to be that excel window
            //then grab application object
            xlWin win = null;
            Int32 hr  = AccessibleObjectFromWindow(handle, DW_OBJECTID, rrid.ToByteArray(), ref win);

            return(win.Application);
        }
        /// <summary>
        /// Find all open Excel workbooks in all Excel processes.
        /// </summary>
        /// <returns>List of open Excel Workbooks</returns>
        /// <remarks>
        /// This method might not be that friendly because it keeps references open. Excel might not close until finalizers are forced.
        /// </remarks>
        internal static IList <ExcelWorkbook> GetAllOpenWorkbooks()
        {
            HashSet <IntPtr>     windows   = new HashSet <IntPtr>();
            List <ExcelWorkbook> workbooks = new List <ExcelWorkbook>();
            Guid           windowGuid      = typeof(ExcelWindow).GUID;
            HashSet <uint> processes       = new HashSet <uint>();

            ISet <IntPtr> mainWindows;

            mainWindows = FindAllWindowsForClass(IntPtr.Zero, MainWindowClass);

            // find all main excel windows
            foreach (IntPtr mainWindow in mainWindows)
            {
                // filter out multiple windows in the same process.
                uint processId;
                GetWindowThreadProcessId(mainWindow, out processId);
                if (processes.Contains(processId))
                {
                    continue;
                }
                processes.Add(processId);

                // find first desktop window
                IntPtr desktopWindow = FindWindowEx(mainWindow, IntPtr.Zero, DesktopWindowClass, null);
                if (desktopWindow != IntPtr.Zero)
                {
                    // find first workbook window
                    IntPtr workbookWindow = FindWindowEx(desktopWindow, IntPtr.Zero, WorkbookWindowClass, null);
                    if (workbookWindow != IntPtr.Zero)
                    {
                        ExcelWindow excelWindow = null;

                        if (AccessibleObjectFromWindow(workbookWindow, OBJID_NATIVEOM, ref windowGuid, out excelWindow) >= 0)
                        {
                            ExcelApplication excelApplication;
                            excelApplication = excelWindow.Application;

                            foreach (ExcelWorkbook workbook in excelApplication.Workbooks)
                            {
                                workbooks.Add(workbook);
                            }
                        }
                    }
                }
            }
            return(workbooks);
        }
    public static void GetReferences(ref Microsoft.Office.Interop.Excel.Application _Application, ref Microsoft.Office.Interop.Excel.Workbook _Workbook)
    {
        EnumChildCallback cb;
        // First, get Excel's main window handle.
        int hwnd = (int)Process.GetCurrentProcess().MainWindowHandle;

        // We need to enumerate the child windows to find one that
        // supports accessibility. To do this, instantiate the
        // delegate and wrap the callback method in it, then call
        // EnumChildWindows, passing the delegate as the 2nd arg.
        if (hwnd != 0)
        {
            int hwndChild = 0;
            cb = new EnumChildCallback(EnumChildProc);
            EnumChildWindows(hwnd, cb, ref hwndChild);
            // If we found an accessible child window, call
            // AccessibleObjectFromWindow, passing the constant
            // OBJID_NATIVEOM (defined in winuser.h) and
            // IID_IDispatch - we want an IDispatch pointer
            // into the native object model.
            if (hwndChild != 0)
            {
                const uint OBJID_NATIVEOM = 0xFFFFFFF0;
                Guid       IID_IDispatch  = new Guid(
                    "{00020400-0000-0000-C000-000000000046}");
                Microsoft.Office.Interop.Excel.Window ptr = null;
                int hr = AccessibleObjectFromWindow(
                    hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref ptr);
                if (hr >= 0)
                {
                    // If we successfully got a native OM
                    // IDispatch pointer, we can QI this for
                    // an Excel Application (using the implicit
                    // cast operator supplied in the PIA).
                    _Application = ptr.Application;
                    _Workbook    = _Application.ActiveWorkbook;
                }
            }
        }
    }
Пример #6
0
    private Microsoft.Office.Interop.Excel.Application SearchExcelInterop(Process p)
    {
        Microsoft.Office.Interop.Excel.Window ptr = null;
        int hwnd       = 0;
        int hWndParent = (int)p.MainWindowHandle;

        if (hWndParent == 0)
        {
            throw new ExcelMainWindowNotFoundException();
        }
        EnumChildWindows(hWndParent, EnumChildFunc, ref hwnd);
        if (hwnd == 0)
        {
            throw new ExcelChildWindowNotFoundException();
        }
        int hr = AccessibleObjectFromWindow(hwnd, DW_OBJECTID, rrid.ToByteArray(), ref ptr);

        if (hr < 0)
        {
            throw new AccessibleObjectNotFoundException();
        }
        return(ptr.Application);
    }
 public static extern int AccessibleObjectFromWindow(
       int hwnd, uint dwObjectID, byte[] riid,
       ref Microsoft.Office.Interop.Excel.Window ptr);
Пример #8
0
 void _ExcelApplication_WindowActivate(Microsoft.Office.Interop.Excel.Workbook Wb, Microsoft.Office.Interop.Excel.Window Wn)
 {
     DisplayInWatchWindow(WindowActivate++, System.Reflection.MethodInfo.GetCurrentMethod().Name);
 }
Пример #9
0
 private static extern Int32 AccessibleObjectFromWindow(
     Int32 hwnd, UInt32 dwObjectID, Byte[] riid, ref xlWin ptr);
Пример #10
0
 public AddinWindow(ThisAddIn thisAddIn)
 {
     window = thisAddIn.Application.ActiveWindow;
 }
 private static extern int AccessibleObjectFromWindow(
     IntPtr handle,
     uint id,
     ref Guid iid,
     out ExcelWindow excelWindow);
Пример #12
0
 private static extern int AccessibleObjectFromWindow(int hwnd, uint dwObjectID, byte[] riid, out xlWin ppvObject);