我有非常类似的问题这
然而,答案也还不是很清楚,我试图在问题重新创建命令按钮,并没有奏效。
基本上我有在模板中,每个板块各部分我有两个按钮
- [添加小节] - (CommandButton1的,CommandButton11,CommandButton111)
- [完成] - (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)来自这个问题前,我才问,只给你一些背景。
在编辑器中我有三个项目
- 正常
- 微软Word对象
- 的ThisDocument - [空]
- 微软Word对象
- 文档1
- 微软Word对象
- 的ThisDocument - [空]
- 参考
- 参考模板项目
- 微软Word对象
- 模板
- 微软Word对象
- 的ThisDocument - [GOT所有的代码]
- 微软Word对象
我尝试手动复制所有的“模板”项目中的代码进入“文档1”项目,然后将其保存为一个DOCM。 这解决了问题,但我不能满足于这个作为[添加小节]基本上将存储在原始模板(这不会是可用的,如果我要邮寄DOCM的人)的构建块。
我欢迎任何解决方案,只要在它结束时我可以邮寄给别人一个文件,他们可以在点击按钮添加章节