2015-03-08 49 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