我有非常相似的VBA宏停止工作後,從DOTM只在DOCM刪除命令按鈕
問題但是答案了,不是很清楚,我試圖重建有問題的命令按鈕,並沒有奏效。
基本上我有在模板內各部分併爲每個部分我有兩個按鈕
- [添加小節] - (CommandButton1的,CommandButton11,CommandButton111)
- [完成] - (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,只是爲了給你一些背景。
在編輯器中我有三個項目
- 普通
- 的Microsoft Word對象
- 的ThisDocument - [空]
- 的Microsoft Word對象
- 文檔1
- 的Microsoft Word對象
- 的ThisDocument - [空]
- 參考
- 參考模板項目
- 的Microsoft Word對象
- 模板
- 的Microsoft Word對象
- 的ThisDocument - [GOT所有代碼]
- 的Microsoft Word對象
我嘗試手動複製所有的代碼在 「模板」投影到「document1」項目中,然後將其另存爲docm。這解決了這個問題,但是我不能解決這個問題,因爲[添加子部分]基本上添加了存儲在原始模板中的構建模塊(如果我要將docm發送給某人,它將不可用)。
我歡迎任何解決方案,只要在它結束時我可以郵寄給別人一個文件,他們可以在一個按鈕
「沒有任何代碼可行」看起來像什麼?如果您在按鈕代碼中放置一箇中斷,當您單擊該按鈕時它會被擊中?嘗試評論'On Error Resume Next'並查看你得到的錯誤(如果有的話)。 – 2015-03-08 22:18:54
好吧,我還有一些其他「inlinehapes」模板中水平線的形式。如果我將'On Error Resume Next'註釋掉,我得到一個「這個成員不能在水平線上訪問」的錯誤。 – TDsouza 2015-03-09 05:17:43