问题描述
我是C#的新手。我正在更改其他人编写的项目,该项目创建了Excel工作簿。我需要创建一个宏并在excel工作簿中运行它。我遇到以下代码问题。
I am very new to C#. I am making changes to a project that someone else wrote which creates an Excel Workbook. I need to create a macro and run it in the excel workbook. I am having problems with the following code.
Excel._Workbook wb = workbook_model.GetExcelWorkbook();
VBIDE.VBComponent module = wb.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
下面的方法返回正确的类型,用作上面的变量''wb'':
The method below returns the correct type to be used as the variable ''wb'' above:
internal Excel._Workbook GetExcelWorkbook()
然而,我得到了
However, I get the red wiggly line under
wb.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);>
下的红色摇摆线和错误消息:
and the error message:
Error: The best overloaded method match for ''Microsoft.Vbe.Interop._VBComponents.Add(Microsoft.Vbe.Interop.vbext_ComponentType)'' has some invalid arguments>
我在网上看过几个例子,这就是他们看起来的样子要做 - 我不能看到我做的不同。我似乎使用了错误的类型,但是我应该使用什么呢?为什么我一直在看的例子在我的情况下不起作用?
非常感谢。
I have looked at several examples on the web and this is what they seem to do - I can''t see what I am doing differently. It seems that I am using the wrong type, but what should I use instead and why do the examples I have been looking at not work in my case?
Many thanks.
推荐答案
Excel._Workbook wb = workbook_model.GetExcelWorkbook();
Microsoft.Vbe.Interop.vbext_ComponentType component_type = Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule;
Microsoft.Vbe.Interop.VBComponent module = wb.VBProject.VBComponents.Add(component_type);>
所以似乎我使用了错误的类型。我使用类型启动VBIDE但应该使用Microsoft.Vbe.Interop类型。我不知道在我的情况下使用VBIDE类型有什么问题,因为示例使用VBIDE。而且,来自C ++,我被告知.net真的很好,因为.net可能只有一个正确的库或类可供使用,而不是可用于执行相似或相同的任务的类和库。这似乎不是这种情况。我很困惑!
So it seems that I used the wrong type. I used types starting VBIDE but should have used the Microsoft.Vbe.Interop types. I don''t know what is wrong woth using the VBIDE types in my case as the examples use VBIDE. And, coming from C++, I have been told that .net is really good because instead of haing umpteen classes and libraries available to do similar or the same things, .net apparantly just has one correct lib or class to use. This does not seem to be the case here. I am confused!
这篇关于尝试从C#代码创建Excel宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!