2012-10-17 68 views
1

我有一個示例代碼在一個Excel工作表vb.net代碼創建按鈕如下:命令按鈕運行時異常

Dim button As Microsoft.Office.Interop.Excel.OLEObject 
button = _xlWorksheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _ 
    Link:=False, DisplayAsIcon:=False, Left:=0, Top:=0, Width:=100, Height:=30) 
button.Name = "Send Mail" 

Dim oCommandBar As Microsoft.Office.Core.CommandBar 
Dim oCommandBarButton As Microsoft.Office.Core.CommandBarControl 
Dim tb As String = "My Toolbar" 
Dim _btnCaption As String = "Forward To BSC" 
'Dim code As String 

'code = "Sub button_Click()" & vbCrLf 
'code = code & "Call Tester" & vbCrLf 
'code = code & "End Sub" 

Try 
    oCommandBar = _xlApp.CommandBars.Add(.......) 

    'oCommandBar = _xlApp.CommandBars.Add(.....) 
    oCommandBar.Visible = True 

    oCommandBarButton = oCommandBar.Controls.Add(button) 
    oCommandBarButton.Caption = _btnCaption 
    oCommandBarButton.OnAction = "button_Click" 
Catch ex As Exception 
    Console.WriteLine(ex.Message) 
End Try 

Sub Tester() 
     MsgBox("You have click on the test button") 
End Sub 

我不知道該怎麼在以下補充:
_xlApp.CommandBars.Add(.......) and _xlApp.CommandBars.Add(.....)

請指教。還指定所需的名稱空間。這是一個控制檯應用程序。

回答