2

相互運用機能を介して Excel と対話する VB.NET で記述されたアプリケーションがあります。最終的に、セル編集モードの既知の問題に遭遇しました (背景については、 MSDNstackoverflowを参照してください)。

提案されたコードを 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 Object Library への (COM) 参照を追加しました

Imports Office = Microsoft.Office.Core
Imports Microsoft.Office.Interop

私はちょっと立ち往生しています。CommandBar オブジェクトを間接的に参照し、参照を再度追加しようとしましたが、何が問題なのかわかりません。何か案は?

4

5 に答える 5

3

簡単な修正として、代わりに次のコードを使用しました

Private Function isEditMode() As Boolean
    isEditMode = False
    Try
        oExcel.GoTo("###")
    Catch Ex As Exception
       ' Either returns "Reference is not valid." 
       ' or "Exception from HRESULT: 0x800A03EC"
       If ex.Message.StartsWith("Exception") then isEditMode  = True
    End Try     
End Function

Excel がセル編集モードの場合、.GoTo 関数 (および対応するメニュー項目) は使用できません。.GoTo 関数にダミーの宛先を指定しても、コードの実行時にユーザーがセルで作業している場合、何も実行されず、何の影響もありません。

さらに、Microsoft Office オブジェクト (Microsoft.Office.Core) ライブラリへの参照は必要ありません。

于 2009-01-21T08:15:22.457 に答える
1
Function ExcelIsBusy()
ExcelIsBusy = Not Application.Ready
Dim m
m = Empty
Const MENU_ITEM_TYPE = 1
Const NEW_MENU = 18

Dim oNewMenu
Set oNewMenu = Application.CommandBars("Worksheet Menu Bar").FindControl(MENU_ITEM_TYPE, NEW_MENU, m, m, True)
If Not (oNewMenu Is Nothing) Then
    If Not oNewMenu.Enabled Then
        ExcelIsBusy = True
        'throw new Exception("Excel is in Edit Mode")
    End If
End If

End Function
于 2012-09-02T18:03:38.563 に答える