【问题标题】:command button run time exception命令按钮运行时异常
【发布时间】:2012-10-07 12:08:37
【问题描述】:

我有一个在 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(.....)

请指教。还要指定所需的命名空间。这是一个控制台应用程序。

【问题讨论】:

    标签: vb.net excel exception-handling


    【解决方案1】:

    我不确定您的问题是什么,但是: 该代码在栏集合中添加了一个新的命令栏。 语法是这样的: CommandBars.Add("MyTemporaryToolbar", _ MsoBarPosition.msoBarTop,假,真); 有关更多信息,以下文章可能对您有用: http://msdn.microsoft.com/en-us/library/office/ff861773.aspx 和 http://www.codeproject.com/Articles/17458/Adding-commandbar-controls-to-outlook

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 2016-04-06
      • 2012-04-10
      • 2015-06-21
      • 1970-01-01
      • 2011-05-30
      相关资源
      最近更新 更多