VBA宏停止工作后,仅在从DOTM DOCM删除命令按钮(VBA macros stop worki

2019-10-22 04:28发布

我有非常类似的问题这

然而,答案也还不是很清楚,我试图在问题重新创建命令按钮,并没有奏效。

基本上我有在模板中,每个板块各部分我有两个按钮

  1. [添加小节] - (CommandButton1的,CommandButton11,CommandButton111)
  2. [完成] - (CommandButton2,CommandButton21,CommandButton211)

一切都在模板中正常工作。

但是,如果我通过在DOTM或右clicking->新的,然后尝试使用按钮或者双击创建一个新文档,它们都运行良好,直到我试试[完成]按钮。 在它工作的首次尝试,帖子里面没有任何代码工作都没有。 下面的代码

        Private Sub CommandButton1_Click()
         Dim objTemplate As Template
         Dim objBB As BuildingBlock

         ' Set the template to store the building block
         Set objTemplate = ActiveDocument.AttachedTemplate

         ' Access the building block through the type and category
         Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustom5) _
         .Categories("General").BuildingBlocks("Experience")

         ' Insert the building block into the document replacing any selected text.
         Selection.MoveUp Unit:=wdLine, Count:=1
         objBB.Insert Selection.Range
        End Sub

        Private Sub CommandButton11_Click()
         Dim objTemplate As Template
         Dim objBB As BuildingBlock

         ' Set the template to store the building block
         Set objTemplate = ActiveDocument.AttachedTemplate

         ' Access the building block through the type and category
         Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustom5) _
         .Categories("General").BuildingBlocks("Experience")

         ' Insert the building block into the document replacing any selected text.
         Selection.MoveUp Unit:=wdLine, Count:=1
         objBB.Insert Selection.Range
        End Sub

        Private Sub CommandButton111_Click()
            Dim objTemplate As Template
         Dim objBB As BuildingBlock

         ' Set the template to store the building block
         Set objTemplate = ActiveDocument.AttachedTemplate

         ' Access the building block through the type and category
         Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustom5) _
         .Categories("General").BuildingBlocks("Education")

         ' Insert the building block into the document replacing any selected text.
         Selection.MoveUp Unit:=wdLine, Count:=1
         objBB.Insert Selection.Range
        End Sub

        Private Sub CommandButton2_Click()

           On Error Resume Next
            Err.Clear

            Dim i As Integer
            i = ActiveDocument.InlineShapes.Count
            Do While (i > 0)
                If ActiveDocument.InlineShapes(i).OLEFormat.ClassType = "Forms.CommandButton.1" Then

                    If ActiveDocument.InlineShapes(i).OLEFormat.Object.Name = "CommandButton1" _
                    Or ActiveDocument.InlineShapes(i).OLEFormat.Object.Name = "CommandButton2" Then

                        If Err.Number = 0 Then
                            ActiveDocument.InlineShapes(i).Delete
                        End If
                        Err.Clear

                    End If

                End If
                i = i - 1
            Loop

        End Sub

        Private Sub CommandButton21_Click()
            On Error Resume Next
            Err.Clear

            Dim i As Integer
            i = ActiveDocument.InlineShapes.Count
            Do While (i > 0)
                If ActiveDocument.InlineShapes(i).OLEFormat.ClassType = "Forms.CommandButton.1" Then

                    If ActiveDocument.InlineShapes(i).OLEFormat.Object.Name = "CommandButton11" _
                    Or ActiveDocument.InlineShapes(i).OLEFormat.Object.Name = "CommandButton21" Then

                        If Err.Number = 0 Then
                            ActiveDocument.InlineShapes(i).Delete
                        End If
                        Err.Clear

                    End If

                End If
                i = i - 1
            Loop
        End Sub

        Private Sub CommandButton211_Click()
            On Error Resume Next
            Err.Clear

            Dim i As Integer
            i = ActiveDocument.InlineShapes.Count
            Do While (i > 0)
                If ActiveDocument.InlineShapes(i).OLEFormat.ClassType = "Forms.CommandButton.1" Then

                    If ActiveDocument.InlineShapes(i).OLEFormat.Object.Name = "CommandButton111" _
                    Or ActiveDocument.InlineShapes(i).OLEFormat.Object.Name = "CommandButton211" Then

                        If Err.Number = 0 Then
                            ActiveDocument.InlineShapes(i).Delete
                        End If
                        Err.Clear

                    End If

                End If
                i = i - 1
            Loop

我是新来的VBA和建造这个由来自各种来源放在一起的各种片段(我知道这可能不是所有的整齐,但已经开始的地方)。 在[完成]代码(commandbutton2,21,211)来自这个问题前,我才问,只给你一些背景。

在编辑器中我有三个项目

  1. 正常
    • 微软Word对象
      • 的ThisDocument - [空]
  2. 文档1
    • 微软Word对象
      • 的ThisDocument - [空]
    • 参考
      • 参考模板项目
  3. 模板
    • 微软Word对象
      • 的ThisDocument - [GOT所有的代码]

我尝试手动复制所有的“模板”项目中的代码进入“文档1”项目,然后将其保存为一个DOCM。 这解决了问题,但我不能满足于这个作为[添加小节]基本上将存储在原始模板(这不会是可用的,如果我要邮寄DOCM的人)的构建块。

我欢迎任何解决方案,只要在它结束时我可以邮寄给别人一个文件,他们可以在点击按钮添加章节

Answer 1:

当使用On Error Resume Next管理预期的问题,最好尽可能限制其范围可能,或运行在你的代码屏蔽其它错误的风险。

例如,您可以通过创建一个“IsButton()”函数是这样的,从您发布的代码删除:

Function Isbutton(s) As Boolean
    Dim f As String
    On Error Resume Next
    f = s.OLEFormat.ClassType
    On Error GoTo 0
    Isbutton = (f = "Forms.CommandButton.1")
End Function

分解出它减少到像这样的重复的代码:

Private Sub CommandButton1_Click()
   InsertSection
End Sub
Private Sub CommandButton11_Click()
   InsertSection
End Sub
Private Sub CommandButton111_Click()
   InsertSection
End Sub


Sub InsertSection()
    Dim objTemplate As Template
    Dim objBB As BuildingBlock

    Set objTemplate = ActiveDocument.AttachedTemplate
    Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustom5) _
    .Categories("General").BuildingBlocks("Experience")

    Selection.MoveUp Unit:=wdLine, Count:=1
    objBB.Insert Selection.Range
End Sub


Private Sub CommandButton2_Click()
    DeleteButtons "CommandButton1", "CommandButton2"
End Sub
Private Sub CommandButton21_Click()
    DeleteButtons "CommandButton11", "CommandButton21"
End Sub
Private Sub CommandButton211_Click()
    DeleteButtons "CommandButton111", "CommandButton211"
End Sub

Private Sub DeleteButtons(Name1 As String, Name2 As String)

    Dim i As Integer, s As InlineShape, nm As String
    i = ActiveDocument.InlineShapes.Count

    Do While (i > 0)
        Set s = ActiveDocument.InlineShapes(i)
        If Isbutton(s) Then
            nm = s.OLEFormat.Object.Name
            Debug.Print i, nm '<<<EDIT
            If nm = Name1 Or nm = Name2 Then s.Delete
        End If
        i = i - 1
    Loop
End Sub


文章来源: VBA macros stop working after delete commandbutton only in docm from dotm