Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);


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.
Posted

1 solution

Ok, I have been trying anything to see what happens. I have ended up with the following code which compiles - I havn't written enough to test it yet.

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);>


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!
 
Share this answer
 
Comments
Maciej Los 27-Mar-13 7:14am    
If this is not an answer, please, delete it and upgrade your question with "Improve question" widget.
Jackie Lloyd 27-Mar-13 7:41am    
Ok, I have been trying anything to see what happens, and found that if i wrote the code as below it works. It compiles and runs correctly.

<pre>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);></pre>

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!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900