使用VS2010,我无法使用VBIDE互操作DLL。有人可以看看他们是否可以在他们的电脑上重现 - 直接来自http://support.microsoft.com/kb/303872
在C#中创建winform应用程序。
引用这两个DLL:
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
在Form_load中输入以下代码:
//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
使用CLSID检索组件的COM类工厂 {BE39F3DA-1B13-11D0-887F-00A0C90F2744}因以下原因失败 错误:80040154未注册类(HRESULT异常: 0x80040154(REGDB_E_CLASSNOTREG))。
答案 0 :(得分:1)
工作代码:
VBIDE.VBComponent oModule;
oModule = excelWorkbook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);