问题描述
我在程序的控件中使用了axWebBrowser这个控件来显示WORD文档,对此使用以下两行代码希望达到对WORD文档操作的效果oApplication=axWebBrowser1.GetType().InvokeMember("Document",System.Reflection.BindingFlags.GetProperty,null,axWebBrowser1,null);//通过反射,获得word应用程序实例Microsoft.Office.Interop.Word.Documentdoc=(Microsoft.Office.Interop.Word.Document)oApplication;//在这一行报错。未处理System.InvalidCastExceptionHResult=-2147467262Message=无法将类型为“mshtml.HTMLDocumentClass”的COM对象强制转换为接口类型“Microsoft.Office.Interop.Word.Document”。此操作失败的原因是对IID为“{0002096B-0000-0000-C000-000000000046}”的接口的COM组件调用QueryInterface因以下错误而失败:不支持此接口(异常来自HRESULT:0x80004002(E_NOINTERFACE))。Source=TestStackTrace:在Test.Form1.openTextPaper_Click(Objectsender,EventArgse)位置d:TestForm1.cs:行号49在System.Windows.Forms.Control.OnClick(EventArgse)在System.Windows.Forms.Button.OnClick(EventArgse)在System.Windows.Forms.Button.OnMouseUp(MouseEventArgsmevent)在System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtonsbutton,Int32clicks)在System.Windows.Forms.Control.WndProc(Message&m)在System.Windows.Forms.ButtonBase.WndProc(Message&m)在System.Windows.Forms.Button.WndProc(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtrhWnd,Int32msg,IntPtrwparam,IntPtrlparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtrdwComponentID,Int32reason,Int32pvLoopData)在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32reason,ApplicationContextcontext)在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32reason,ApplicationContextcontext)在System.Windows.Forms.Application.Run(FormmainForm)在Test.Program.Main()位置d:TestProgram.cs:行号18在System.AppDomain._nExecuteAssembly(RuntimeAssemblyassembly,String[]args)在System.AppDomain.ExecuteAssembly(StringassemblyFile,EvidenceassemblySecurity,String[]args)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()在System.Threading.ThreadHelper.ThreadStart_Context(Objectstate)在System.Threading.ExecutionContext.RunInternal(ExecutionContextexecutionContext,ContextCallbackcallback,Objectstate,BooleanpreserveSyncCtx)在System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallbackcallback,Objectstate,BooleanpreserveSyncCtx)在System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallbackcallback,Objectstate)在System.Threading.ThreadHelper.ThreadStart()InnerException:程序在2003版的文档下是可以运行的,但是在高级版本中不行,除了下载兼容包等用户需要完成的解决方法之外,有什么样简单的方法能从代码上解决根本问题,让该程序能够适应各个版本。