title: Deleting CommandButtons by name in VBA Word 2010
body: <p>In a word template, I have two command buttons namely </p>
  
  <ol>
  <li><p><strong>[Add section]</strong> which is named "CommandButton1"</p></li>
  <li><p><strong>[Done]</strong> - which is named "CommandButton2"</p></li>
  </ol>
  
  <p>I need both buttons to be deleted at the click of [Done]</p>
  
  <p>Here's my code so far</p>
  
  <pre><code>Private Sub CommandButton2_Click()
  
      Dim i As Integer
  
      For i = ThisDocument.InlineShapes.Count To 1 Step -1
  
        With ThisDocument.InlineShapes(i)
            If .OLEFormat.Object.Name = "CommandButton1" Then
                .Delete
            End If
         End With
     Next
  
  End Sub
  </code></pre>
  
  <p>The code is a combination of snippets I found online. I had only added "CommandButton1" for testing purpose and was planning to add a check for CommandButton2 as well if this works.</p>
  
  <p>On executing the code it successfully deletes CommandButton2 and gives me an error "<em>Objected cannot be accessed on a horizontal line</em>" without deleting CommandButton1. </p>
  
  <p>I tried messing around with the "count to 1 Step -1" part (I don't know what they imply), but it all results in the same thing.</p>