如何从C#代码调用宏? [英] How to call macro from C# code ?

查看:472
本文介绍了如何从C#代码调用宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从c#代码调用excel中的宏时,我得到此异常System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC



我尝试过:



我使用Microsoft.Office.Interop.Excel版本15,客户端机器有Office 2000,在调用宏时,我得到以下异常System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC。



这是我有的代码。



While calling macro in excel from c# code , i get this exception System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC

What I have tried:

I use Microsoft.Office.Interop.Excel version 15 and the client machine has Office 2000 and while calling macro , i get the following exception System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC.

This is the code i have trid.

private void RunMacro(Application xlApp, Workbook xlWB, string MacroName, out string status)
       {
           Worksheet xlWS;
           status = string.Empty;
           if (xlApp != null)
           {

               xlWS = (Excel.Worksheet)xlWB.Worksheets.get_Item("Sheet1");// .get_Item(paramsExcelOperatios.WorksheetNo);
               if (MacroName != String.Empty)
               {
                   xlWS.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, xlApp, new Object[] { MacroName });

               }
               else
               {
                   status = "Failure - Macro name should not be empty";
               }

           }
           else
           {
               status = "Failure - Worksheet name is not valid.";
           }
       }

推荐答案

这是个坏主意,糟糕的做法!



任何VBA代码都可以用Excel对象做,C#也可以这样做。注意,taht C#比VBA提供更多,例如:Linq。
This is bad idea, bad approach!

Anything VBA code can do with Excel objects, C# is able to do the same. Note, taht C# offers much more than VBA, for example: Linq.


这篇关于如何从C#代码调用宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆