在word轉換成html的時候,由於系統版本不一樣,office總是拋出異常,Microsoft Word停止工作,下面有三個按鈕,關閉程序等等,但是我的轉換工作需要自動的,每當拋出異常的時候我的程序就會停止工作,對此就需要自動關閉這個異常,需要以下的這個代碼!
--相應的引用 [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern int FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll", EntryPoint = "FindWindowEx")] private static extern int FindWindowEx(IntPtr lpClassName, IntPtr lpWindowName, string isnull, string anniu); [DllImport("user32.dll", EntryPoint = "SendMessageA")] private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); private void test() { const int WM_CLOSE = 0x10; //關閉 const uint WM_DESTROY = 0x02; const uint WM_QUIT = 0x12; const int BM_CLICK = 0xF5; //單擊 IntPtr Window_Handle = (IntPtr)FindWindow(null, "Microsoft Word");//查找所有的窗體,看看想查找的句柄是否存在,Microsoft Word 句柄 // if (Window_Handle ==IntPtr.Zero) //如果沒有查找到相應的句柄 { MessageBox.Show("沒有找到窗體"); } else //查找到相應的句柄 { SendMessage(Window_Handle, WM_CLOSE, 0, 0); //關閉窗體 // IntPtr childHwnd = (IntPtr)FindWindowEx(Window_Handle, IntPtr.Zero, null, "點擊");//查找句柄中相應的按鈕 // if (childHwnd == IntPtr.Zero) // { // MessageBox.Show("沒有找到按鈕"); // } // else // { // SendMessage(childHwnd, BM_CLICK, 0, 0); //單擊這個句柄中的按鈕 // } } }