VBA宏仅在dotm的docm中删除命令按钮后停止工作 [英] VBA macros stop working after delete commandbutton only in docm from dotm
问题描述
我遇到的问题与此
但是答案还不是很明确,我尝试重新创建有问题的命令按钮,但是它没有用.
However the answer there is not very clear, and I tried recreating the commandbutton in question, and it did not work.
基本上,模板中有多个部分,每个部分都有两个按钮
Basically I have various sections within the template and for each section I have two buttons
- [添加小节] -(CommandButton1,CommandButton11,CommandButton111)
- [完成] -(CommandButton2,CommandButton21,CommandButton211)
- [Add sub-section] - (CommandButton1, CommandButton11, CommandButton111)
- [Done] - (CommandButton2, CommandButton21, CommandButton211)
模板中的所有内容都可以正常工作.
Everything works fine in the template.
但是,如果我通过双击dotm或右键单击-> new来创建新文档,然后尝试使用按钮,则它们都运行良好,直到我尝试了[Done]按钮之一.第一次尝试有效时,发布任何代码都无法正常工作.这是代码
But if I create a new doc by either double clicking on the dotm or right clicking->new and then try using the buttons, they all run well, until I try one of the [Done] buttons. At the first attempt it works, post which no code works what so ever. Here's the code
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)来自这个问题我刚才问过,只是为了给您一些背景信息.
I'm new to VBA and built this by putting together various snippets from various sources ( I know it may not be all that neat, but had to start somewhere). The [Done] code (commandbutton2,21,211) came from this question I had asked earlier, just to give you some context.
在编辑器中,我有三个项目
In the editor I have three projects
- 普通
- Microsoft Word对象
- 此文档-[空]
- Microsoft Word对象
- Normal
- Microsoft Word Objects
- ThisDocument - [Empty]
- Microsoft Word Objects
- Microsoft Word对象
- 此文档-[空]
- Microsoft Word Objects
- ThisDocument - [Empty]
- 参考模板项目
- Microsoft Word对象
- 此文档-[获取所有代码]
- Microsoft Word Objects
- ThisDocument - [Got all the code]
我尝试手动将模板"项目中的所有代码复制到"document1"项目中,然后将其另存为docm.这解决了问题,但是我不能满足要求,因为[添加小节]基本上添加了存储在原始模板中的构造块(如果我将docm邮寄给某人,则将不可用).
I tried manually copying all of the code in "template" project into the "document1" project and then saving it as a docm. This fixed the problem, however I can't settle for this as [Add sub-section] basically adds a building block stored in the original template(which wont be available if I were to mail the docm to someone).
只要有一个解决方案,只要我有一个可以邮寄给某人的文件,他们只要单击一下按钮,便可以添加部分
I'm open to any solution as long as at the end of it I have a file that can be mailed to someone and they could add sections at the click of a button
推荐答案
使用
On Error Resume Next
管理预期的问题时,最好尽可能地限制其范围,否则冒着掩盖代码中其他错误的风险.When using
On Error Resume Next
to manage an anticipated problem it's best to limit its scope as much as possible, or you run the risk of masking other errors in your code.例如,您可以通过创建类似于以下内容的"IsButton()"函数将其从发布的代码中删除:
For example, you can remove it from your posted code by creating an "IsButton()" function something like this:
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
根据重复的代码将其简化为以下形式:
Factoring out the repeated code it reduces to something like this:
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
这篇关于VBA宏仅在dotm的docm中删除命令按钮后停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!