/// <summary>
        /// Try to detect the main window handle for a document instance
        /// </summary>
        /// <param name="document">target document instance</param>
        /// <returns>main window handle or 0 if failed</returns>
        public int TryGetMainWindowHandle(WordApi.Document document)
        {
            if (null == document)
            {
                throw new ArgumentNullException("document");
            }
            int hwnd = TryGetHostApplicationWindowHandle(document);

            return(hwnd);
        }
        private int TryGetHostApplicationWindowHandleFromDesktop(WordApi.Document document)
        {
            try
            {
                int result = 0;
                Running.WindowEnumerator enumerator = new Running.WindowEnumerator("OpusApp");
                IntPtr[] handles = enumerator.EnumerateWindows(2000);

                foreach (IntPtr item in handles)
                {
                    object proxyDocument = GetAccessibleObject(item);
                    if (null != proxyDocument)
                    {
                        try
                        {
                            bool equals = Equal(document.UnderlyingObject, proxyDocument);
                            if (equals)
                            {
                                result = (int)item;
                            }
                            break;
                        }
                        catch
                        {
                            throw;
                        }
                        finally
                        {
                            Marshal.ReleaseComObject(proxyDocument);
                        }
                    }
                }

                return(result);
            }
            catch (Exception exception)
            {
                NetOffice.Core.Default.Console.WriteException(exception);
                return(0);
            }
        }
        /// <summary>
        /// Try to detect the associated main window from a document has an expanded ribbon ui
        /// </summary>
        /// <param name="document">target document</param>
        /// <param name="throwExceptionIfFailed">throw exception if its failed to detect, otherwise returns false</param>
        /// <returns>true if ribbon is expanded, otherwise false</returns>
        public bool TryGetRibbonIsExpanded(WordApi.Document document, bool throwExceptionIfFailed)
        {
            if (null == document)
            {
                throw new ArgumentNullException("document");
            }
            int handle = TryGetMainWindowHandle(document);

            if (handle <= 0)
            {
                if (throwExceptionIfFailed)
                {
                    throw new NetRuntimeSystem.ComponentModel.Win32Exception();
                }
                else
                {
                    return(false);
                }
            }

            Running.ChildWindowEnumerator childEnumerator = new Running.ChildWindowEnumerator(new IntPtr(handle), "Ribbon");
            IntPtr[] handles = childEnumerator.EnumerateWindows(2000);
            if (null != handles && handles.Length > 0)
            {
                NetRuntimeSystem.Drawing.Rectangle rect = Running.WindowEnumerator.GetWindowRect(handles[0]);
                return(rect.Height >= _ribbonHeightExpandedLimit);
            }
            else
            {
                if (throwExceptionIfFailed)
                {
                    throw new NetRuntimeSystem.Runtime.InteropServices.ExternalException();
                }
                else
                {
                    return(false);
                }
            }
        }
        private int TryGetHostApplicationWindowHandle(WordApi.Document document)
        {
            int result = TryGetHostApplicationWindowHandleFromDesktop(document);

            return(result);
        }