【发布时间】:2011-02-08 15:12:29
【问题描述】:
我创建了一个简单的应用程序 .net 类,可将 Excel 电子表格转换为 pdf 文件。然后我得到一个 Excel 2007 应用程序来调用这个在我的开发机器上运行良好的 dll。
但是,当我将它部署到同时具有 .net 框架和 Excel 2007 的 Vista 机器上时,我收到此错误:
run-time error '429' activex component can't create object
即使我是机器上的管理员,我似乎也无法将签名的 .net dll 放入 GAC。
有人可以帮我解决这个问题吗?
这就是我从 Excel 2007 调用 .net tlb 文件的方式。
Sub TestSub()<br>
Dim printLibraryTest As PrintLibrary.Print<br>
Set printLibraryTest = New PrintLibrary.Print <br>
printLibraryTest.ConvertExcelToPdf <br>
End Sub <br>
这是下面的 .net 库。
using System;<br>
using System.Collections.Generic;<br>
using System.Runtime.InteropServices;<br>
using System.Text;<br>
using Microsoft.Office.Interop.Excel;<br><br>
namespace PrintLibrary<br>
{<br>
[ClassInterface(ClassInterfaceType.None)]<br>
[Guid("3d0f04d2-9123-48e0-b12f-6c276ff2281b")]<br>
[ProgId("PrintLibrary.Test")]<br>
public class Print<br>
{ <br>
public void ConvertExcelToPdf(string inputFile,string outputFile) <br>
{ <br>
ApplicationClass excelApplication = new ApplicationClass(); <br>
Workbook excelWorkBook = null; <br>
string paramSourceBookPath = inputFile; <br>
object paramMissing = Type.Missing; <br>
string paramExportFilePath = outputFile;
XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
XlFixedFormatQuality paramExportQuality =
XlFixedFormatQuality.xlQualityStandard;
bool paramOpenAfterPublish = false;
bool paramIncludeDocProps = true;
bool paramIgnorePrintAreas = true;
object paramFromPage = Type.Missing;
object paramToPage = Type.Missing;
try
{
// Open the source workbook.
excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath,
paramMissing, paramMissing, paramMissing, paramMissing,
paramMissing, paramMissing, paramMissing, paramMissing,
paramMissing, paramMissing, paramMissing, paramMissing,
paramMissing, paramMissing);
// Save it in the target format.
if (excelWorkBook != null)
excelWorkBook.ExportAsFixedFormat(paramExportFormat,
paramExportFilePath, paramExportQuality,
paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage,
paramToPage, paramOpenAfterPublish,
paramMissing);
}
catch (Exception ex)
{
// Respond to the error.
}
终于
{
// Close the workbook object.
if (excelWorkBook != null)
{
excelWorkBook.Close(false, paramMissing, paramMissing);
excelWorkBook = null;
}
// Quit Excel and release the ApplicationClass object.
if (excelApplication != null)
{
excelApplication.Quit();
excelApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
}
【问题讨论】:
-
我假设您的“PrintLibrary.dll”是使用 .NET 构建的?它有任何 COM 可见的类或接口吗?如果没有,则无需注册。如果是这样,我假设您正在使用 RegAsm 注册它,或者您是否(错误地)尝试使用 RegSvr32?当您运行代码并得到“运行时错误'429'activex组件无法创建对象”时,它是哪一行代码? (也就是说,它试图创建什么对象——你确定它来自你的'PrintLibrary.dll吗?)
-
嗨,迈克,感谢您回复我。我创建了对 Visual Studio 创建的 .net tlb 库的引用。它似乎打破了这条线...... printLibraryTest.ConvertExcelToPdf。即使 Visual Studio 创建了一个 tlb 库,我还需要使用 Regasm 吗?我也在使用ClassInterface(ClassInterfaceType.None,所以没有为类生成类接口。
标签: c# .net excel vba excel-2007