无法运行指定的宏Word 2010和c#

时间:2012-01-17 13:17:42

标签: c# com ms-word com-interop office-interop

我正在尝试使用c#运行一个单词宏。

object oMissing = System.Reflection.Missing.Value;


           // Create an instance of Word, make it visible,
           // and open Doc1.doc.
           ApplicationClass oWord = new ApplicationClass();
           oWord.Visible = true;
           Documents oDocs = oWord.Documents;
           object oFile = @"E:\doc1.doc";

           // If the Microsoft Word 10.0 Object Library is referenced
           // use the following code.
           //_Document oDoc = oDocs.Open(ref oFile, ref oMissing,
           //    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
           //    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
           //    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
           //    ref oMissing);

           // If the Microsoft Word 11.0 Object Library is referenced comment
           // the previous line of code and uncomment the following code.
           _Document oDoc = oDocs.Open(ref oFile, ref oMissing,
           ref oMissing, ref oMissing, ref oMissing, ref oMissing,
           ref oMissing, ref oMissing, ref oMissing, ref oMissing,
           ref oMissing, ref oMissing, ref oMissing, ref oMissing,
           ref oMissing, ref oMissing);

           // Run the macros.
           RunMacro(oWord, new Object[] { "ExternalizeFromApp" });


           // Quit Word and clean up.
           oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
           System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
           oDoc = null;
           System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocs);
           oDocs = null;
           oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
           System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
           oWord = null;

和RunMacro方法

 private void RunMacro(object oApp, object[] oRunArgs)
    {
        oApp.GetType().InvokeMember("Run",
            System.Reflection.BindingFlags.Default |
            System.Reflection.BindingFlags.InvokeMethod,
            null, oApp, oRunArgs);
    }

宏位于附加到文档的模板中。但我得到Unable运行指定的宏,内部异常没有任何内容。如果我去记录自己并手动运行宏,它就可以工作。

更新:

发现了System.Reflection.TargetInvocationException   Message =“调用目标抛出了异常。”   来源= “mscorlib程序”   堆栈跟踪:        at System.RuntimeType.InvokeDispMethod(String name,BindingFlags invokeAttr,Object target,Object [] args,Boolean [] byrefModifiers,Int32 culture,String [] namedParameters)        at System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target,Object [] providedArgs,ParameterModifier [] modifiers,CultureInfo culture,String [] namedParams)        at System.Type.InvokeMember(String name,BindingFlags invokeAttr,Binder binder,Object target,Object [] args)        at IMGDoc2Pdf.Main.RunMacro(Object oApp,Object [] oRunArgs)位于C:\ Users \ xxx \ yyy \ Visual Studio 2008 \ Projects \ Project1 \ Project \ app \ Main.cs:第639行   InnerException:System.Runtime.InteropServices.COMException        HELPLINK = “#wdmain11.chm 37401”        Message =“无法运行指定的宏”        来源=“Microsoft Word”        错误码= -2147352573        InnerException:

谁能告诉我如何解决这个问题?

0 个答案:

没有答案