我有非常相似的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