我有一个用 VB.NET 编写的应用程序,它通过互操作与 Excel 进行交互。我最终遇到了 Cell-edit mode 的已知问题(有关一些背景信息,请参阅MSDN和stackoverflow)。
我一直在尝试将建议的代码转换为 VB.NET,但不断收到以下错误:
Reference required to assembly 'office, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' containing the type 'Microsoft.Office.Core.CommandBars'. Add one to your project. (BC30652) - E:\ ... .vb:3471
原始 C# 代码(来自前面提到的文章)如下
private bool IsEditMode()
{
object m = Type.Missing;
const int MENU_ITEM_TYPE = 1;
const int NEW_MENU = 18;
// Get the "New" menu item.
CommandBarControl oNewMenu = Application.CommandBars["Worksheet Menu Bar"].FindControl(MENU_ITEM_TYPE, NEW_MENU, m, m, true );
if ( oNewMenu != null )
{
// Check if "New" menu item is enabled or not.
if ( !oNewMenu.Enabled )
{
return true;
}
}
return false;
}
我转换的 VB.NET 代码如下
Private Function isEditMode() As Boolean
isEditMode = False
Dim m As Object = Type.Missing
Const MENU_ITEM_TYPE As Integer = 1
Const NEW_MENU As Integer = 18
Dim oNewMenu As Office.CommandBarControl
' oExcel is the Excel Application object
' the error is related to the below line
oNewMenu = oExcel.CommandBars("Worksheet Menu Bar").FindControl(MENU_ITEM_TYPE, NEW_MENU, m, m, True)
If oNewMenu IsNot Nothing Then
If Not oNewMenu.Enabled Then
isEditMode = True
End If
End If
End Function
我添加了对 Microsoft Office 对象库的 (COM) 引用
Imports Office = Microsoft.Office.Core
Imports Microsoft.Office.Interop
我有点卡住了。我已经尝试过间接引用 CommandBar 对象,并重新添加引用但无法弄清楚问题所在。有任何想法吗?