using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using xlApp = Microsoft.Office.Interop.Word.Application;
using xlWin = Microsoft.Office.Interop.Word.Window;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static xlApp InnerFromProcess(Process p)
        {
            return InnerFromHandle(ChildHandleFromMainHandle(p.MainWindowHandle.ToInt32()));
        }

        private static Int32 ChildHandleFromMainHandle(Int32 mainHandle)
        {
            Int32 handle = 0;
            EnumChildWindows(mainHandle, EnumChildFunc, ref handle);
            return handle;
        }

        #region Constants & delegates
        private static Boolean EnumChildFunc(Int32 hwndChild, ref Int32 lParam)
        {
            var buf = new StringBuilder(128);
            GetClassName(hwndChild, buf, 128);
            if (buf.ToString() == ComClassName)
            {
                lParam = hwndChild;
                return false;
            }
            return true;
        }

        private const String ComClassName = "_WwG";

        private const UInt32 DW_OBJECTID = 0xFFFFFFF0;

        private const UInt32 GW_HWNDPREV = 3;
        //3 = GW_HWNDPREV
        //The retrieved handle identifies the window above the specified window in the Z order.
        //If the specified window is a topmost window, the handle identifies a topmost window.
        //If the specified window is a top-level window, the handle identifies a top-level window.
        //If the specified window is a child window, the handle identifies a sibling window.

        private static Guid rrid = new Guid("{00020400-0000-0000-C000-000000000046}");

        private delegate Boolean EnumChildCallback(Int32 hwnd, ref Int32 lParam);
        #endregion
        #region Extern Methods

        [DllImport("Oleacc.dll")]
        private static extern Int32 AccessibleObjectFromWindow(
            Int32 hwnd, UInt32 dwObjectID, Byte[] riid, ref xlWin ptr);

        [DllImport("User32.dll")]
        private static extern Boolean EnumChildWindows(
            Int32 hWndParent, EnumChildCallback lpEnumFunc, ref Int32 lParam);

        [DllImport("User32.dll")]
        private static extern Int32 GetClassName(
            Int32 hWnd, StringBuilder lpClassName, Int32 nMaxCount);

        [DllImport("User32.dll")]
        private static extern IntPtr GetWindow(IntPtr hWnd, UInt32 uCmd);

        #endregion


        private const uint OBJID_NATIVEOM = 0xFFFFFFF0;

        [DllImport("Oleacc.dll")]
        private static extern int AccessibleObjectFromWindow(
       int hwnd, uint dwObjectID, byte[] riid,
       ref Word.Application ppvObject);

        private static xlApp InnerFromHandle(Int32 handle)
        {
            xlWin win = null;
            Int32 hr = AccessibleObjectFromWindow(handle, DW_OBJECTID, rrid.ToByteArray(), ref win);
            if (win == null)
            {
                xlApp wordApp = null;
                hr = AccessibleObjectFromWindow(handle, OBJID_NATIVEOM, null, ref wordApp);
                if (hr != 0)
                {
                    return null;
                }
                else
                {
                    return wordApp;
                }
            }
            else
                return win.Application;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string[] processNames = { "winword", "WINWORD", "Microsoft Word", "Word" };
            Process[] processes = null;
            foreach (string processName in processNames)
            {
                processes = Process.GetProcessesByName(processName);
                if (processes.Length > 0)
                {
                    break;
                }
            }
            try
            {
                var WordObj = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
                if (WordObj != null)
                {
                    if (WordObj.Documents.Count > 0)
                    {
                        MessageBox.Show("代码二:" + WordObj.Documents.Count.ToString());
                    }
                    else
                    {
                        MessageBox.Show("代码二:获取失败!");
                    }
                }

                if (processes.Count() > 0)
                {
                    foreach (Process p in processes)
                    {
                        if (p.MainWindowHandle.ToInt32() > 0)
                        {
                            Word.Application oWordApp = InnerFromProcess(p);
                            if (oWordApp != null)
                            {
                                MessageBox.Show("代码一:" + oWordApp.Documents.Count.ToString());
                            }
                            else
                            {
                                MessageBox.Show("代码一:获取失败!");
                            }
                        }
                    }
                }
            }catch (Exception ex){
                MessageBox.Show(ex.ToString());
            }          
        }
    }
}

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐