2015-03-08 80 views
0

我有非常相似的​​VBA宏停止工作后,从DOTM只在DOCM删除命令按钮

问题但是答案了,不是很清楚,我试图重建有问题的命令按钮,并没有奏效。

基本上我有在模板内各部分并为每个部分我有两个按钮

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

一切工作正常在模板中。

但是,如果我通过双击dotm或右击 - > new创建一个新文档,然后尝试使用这些按钮,它们都运行良好,直到我尝试其中一个[完成]按钮。在第一次尝试的时候,没有任何代码能够工作。下面的代码

 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和建造这个由来自各种来源放在一起的各种片段(我知道这可能不是所有的整齐,但已经开始的地方)。 [done]代码(commandbutton2,21,211)来自我之前问过的this question,只是为了给你一些背景。

在编辑器中我有三个项目

  1. 普通
    • 的Microsoft Word对象
      • 的ThisDocument - [空]
  2. 文档1
    • 的Microsoft Word对象
      • 的ThisDocument - [空]
    • 参考
      • 参考模板项目
  3. 模板
    • 的Microsoft Word对象
      • 的ThisDocument - [GOT所有代码]

我尝试手动复制所有的代码在 “模板”投影到“document1”项目中,然后将其另存为docm。这解决了这个问题,但是我不能解决这个问题,因为[添加子部分]基本上添加了存储在原始模板中的构建模块(如果我要将docm发送给某人,它将不可用)。

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

+0

“没有任何代码可行”看起来像什么?如果您在按钮代码中放置一个中断,当您单击该按钮时它会被击中?尝试评论'On Error Resume Next'并查看你得到的错误(如果有的话)。 – 2015-03-08 22:18:54

+0

好吧,我还有一些其他“inlinehapes”模板中水平线的形式。如果我将'On Error Resume Next'注释掉,我得到一个“这个成员不能在水平线上访问”的错误。 – TDsouza 2015-03-09 05:17:43

回答

0

即可添加部分使用On Error Resume Next来管理预期的问题最好是尽可能限制其范围,否则可能会掩盖代码中的其他错误。

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 

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

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

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 
+0

感谢大家简化我的代码,更容易维护,但问题仍然存在。基本上它在dotm中按照预期工作,但是如果创建一个新的docm,它会按下删除按钮的点击 – TDsouza 2015-03-09 06:46:14

+0

它如何“中断”?任何错误消息? – 2015-03-09 06:46:58

+0

没有错误消息,在任何删除按钮的第一次点击它按预期工作,后所有按钮什么都不做 – TDsouza 2015-03-09 06:49:07