Adding MAcros using C#

S

shree

I am having an exception while adding an Excel Macro using C#. I use the
following code to add a macro.

1. Excel.Application xl = null;
2. Excel._Workbook wb = null;
3. Excel._Worksheet sheet = null;
4. VBIDE.VBComponent module = null;
5. string Macro = "assing a macro"; //to be done
6. xl = new Excel.Application();
7. xl.Visible = false;

8. wb = (Excel._Workbook)(xl.Workbooks.Add( Missing.Value ));
9. sheet = (Excel._Worksheet)wb.ActiveSheet;
10. module =
wb.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
11. module.CodeModule.AddFromString(Macro);

Line number 10 throws an exception of type
System.Runtime.InteropServices.COMException with the message: Exception from
HRESULT: 0x800A03EC

Can someone give an idea what I am doing wrong here.

thanks,
Shrish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top