Reputation: 938
I have a problem very similar to this
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
Everything works fine in the template.
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
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
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
Upvotes: 1
Views: 555
Reputation: 166835
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.
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
Upvotes: 0