当前位置: 代码迷 >> VBA >> 书中的示例有异常? commandbar.add 无法执行
  详细解决方案

书中的示例有异常? commandbar.add 无法执行

热度:9210   发布时间:2013-02-26 00:00:00.0
书中的示例有错误? commandbar.add 无法执行?
VB code
Const TOOLBARNAME As String = "工资条生成器"Private Sub Workbook_AddinInstall()Dim tbar As CommandBarDim btn As CommandBarButton'如果已经存在工具栏按钮则删除旧的On Error Resume NextCommandBars(TOOLBARNAME).DeleteOn Error GoTo 0'创建新工具栏'''''''''''''''''''''''''''''''''这里执行不下去''''''''''''''''''''''提示对象变量或with块变量未设置,为啥?Set tbar = CommandBars.AddWith tbar    .Name = TOOLBARNAME    .Visible = TrueEnd With'添加一个按钮Set btn = tbar.Controls.Add(Type:=msoControlButton)With btn    btn.FaceId = 300    btn.OnAction = "main"    btn.Caption = "点此启动工资条生成器"End WithEnd Sub


------解决方案--------------------------------------------------------
Set tbar = Application.CommandBars.Add

加上Application就可以了,

有时书上的例子也不一定正确