2

当引用 Microsoft 的互操作程序集时,可以将 Excel 对象(如 Worksheet、Workbook、Range 等)作为参数从 VBA 传递到 .net:

.NET 类:

using Excel = Microsoft.Office.Interop.Excel;

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MyExcelLib
{
    public MyExcelLib() {}  // default constructor

    public void InsertTable(Excel.Workbook wbook, string TableName, ...)
    {
       ...
    }
}

.. 使用 Visual Studio 中的Register for COM Interop选项或 Regasm 实用程序编译(请参阅Newman 的文章以获得很好的概述)。

VBA 脚本(excel 宏)调用 .net 方法InsertTable()

Dim xLib As New MyExcelLib
Call xLib.InsertTable(ThisWorkbook, "Price", ..)

这行得通;工作簿对象可以作为参数从 VBA 传递到 .NET。但相同的架构不适用于 NetOffice 程序集:

using Excel = NetOffice.ExcelApi;
using NetOffice.ExcelApi.GlobalHelperModules;
using NetOffice.ExcelApi.Enums;

据我了解,这些程序集的Embed Interop Types属性不能设置为 true,因为这会导致编译错误。显然,无法使用 NetOffice 将 excel 对象作为参数传递。这是真的,还是有办法用 NetOffice 做到这一点?

4

1 回答 1

0

Use the native proxy instead of the NO wrapper as first argument in your library method. Something like this:

public void InsertTable(object wbook, string TableName, ...)
{
    Excel.Workbook myBook = new Excel.Workbook(null, wbook);

    myBook.Dispose();
}
于 2015-02-13T15:48:46.630 回答