Jeremy Thompson Jeremy Thompson - 1 year ago 271
C# Question

C# - Class not registered - creating a Excel spreadsheet with Macros using VBIDE

Using VS2010, I cannot use the VBIDE interop DLL. Could someone please see if they can reproduce on their PC - code straight out of

  1. Create winform app in C#.

  2. Reference these 2 DLLs:
    C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Vbe.Interop.dll
    C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll

  3. In Form_load put this code:

//Using statments
using Excel = Microsoft.Office.Interop.Excel;    
using VBIDE = Microsoft.Vbe.Interop;
using System.Reflection;

Excel.Application excelApp = null;
Excel._Workbook excelWorkbook;
Excel._Worksheet excelSheet;
excelApp = new Excel.Application();
excelApp.Visible = true;
excelWorkbook = (Excel._Workbook)(excelApp.Workbooks.Add(Missing.Value));
excelSheet = (Excel._Worksheet)excelWorkbook.ActiveSheet;
VBIDE.VBComponent oModule = new VBIDE.VBComponent(); //<--BAM throws an error

Retrieving the COM class factory for component with CLSID
{BE39F3DA-1B13-11D0-887F-00A0C90F2744} failed due to the following
error: 80040154 Class not registered (Exception from HRESULT:
0x80040154 (REGDB_E_CLASSNOTREG)).

Answer Source

Working code:

VBIDE.VBComponent oModule;

oModule = excelWorkbook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download