当前位置:编程学习 > VB >>

按钮添加不进去

实时错误‘02147221163(80040155)’:
  对象‘CommandBars’的方法‘-Application’失败
程序代码:
Dim Obj_T  As office.CommandBar
Dim Obj_M As office.CommandBarPopup
Dim Obj_Toolbar_button  As office.CommandBarButton

Sub addbutton()
    Set Obj_Toolbar = Application.CommandBars.Add("My_Custom_Bar")
    Set Obj_Menu = Obj_Toolbar.Controls.Add(Type:=msoControlPopup, Id:=1)
    With Obj_Menu
        .Caption = "风格切换"
        .BeginGroup = True
    End With
    With Obj_Toolbar
        .Visible = True
        .Enabled = True
        .Position = msoBarTop
    End With
End Sub
Sub deletebutton() '删除工具栏和菜单的子程序
    Dim tempbar As office.CommandBar  '定义临时工具栏变量
    On Error Resume Next '该语句用于忽略错误
    Application.CommandBars("Menu Bar").Reset '重新设置Word XP的主菜单,即删除新建的菜单
    For Each tempbar In Application.CommandBars '通过"For Each…Next"语句遍历Word XP所有的工具栏
       If tempbar.Name = "My_Custom_Bar" Then '如名称和新建的工具栏相同
           tempbar.Visible = False '设置为不可视
            tempbar.Delete '删除该工具栏
       End If
    Next
End Sub
Private Sub Command1_Click()
Dim myDoc, WordApp As Object
Dim s1 As String
Set WordApp = CreateObject("Word.Application")
Set myDoc = WordApp.Documents.Add()
s1 = "Dim cstmAccounting" & vbCr & _
    "Sub a()" & vbCr & _
    "Set cstmAccounting = CommandBars(""Menu Bar"").Controls.Add(Type:=msoControlPopup, Before:=1)" & vbCr & _
    "cstmAccounting.Caption = ""&Accounting""" & vbCr & _
    "End Sub" & vbCr & _
    "Sub b()" & vbCr & _
    "CommandBars(""Menu Bar"").Controls(""&Accounting"").Delete" & vbCr & _
    "End Sub"
myDoc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString s1
DoEvents
deletebutton
addbutton
On Error Resume Next
myDoc.SaveAs "D:\pit.doc"
WordApp.Visible = True
End Sub



各位大侠:谁来帮我解决下啊 一运行就提示错误,求救,引用的东西都在啊 --------------------编程问答-------------------- Application属性没有对象 --------------------编程问答-------------------- 那是 VBA 的功能。 
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,