Using VS2010, I cannot use the VBIDE interop DLL. Could someone please see if they can reproduce on their PC - code straight out of http://support.microsoft.com/kb/303872
Create winform app in C#.
Reference these 2 DLLs:
C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Offic开发者_如何学Pythone14\Microsoft.Vbe.Interop.dll
andC:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll
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)).
Working code:
VBIDE.VBComponent oModule;
oModule = excelWorkbook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
精彩评论