• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 747
  • Last Modified:

Code to Change Command Button Name

I'm using the following code that was provided by GrahamSkan:

Dim ilsh As InlineShape
Dim i As Integer
For Each ilsh In ActiveDocument.InlineShapes
    If ilsh.OLEFormat.ClassType = "Forms.CommandButton.1" Then
        i = i + 1
        ilsh.OLEFormat.Object.Name = "CB" & i
    End If
Next ilsh

This code used to work, but now I'm getting the following error:
Run-time error '-2147319764 ...
Method 'Name' of object 'ICommandButton' failed

I'm using the code in a macro in a Word 2003 file.  Basically, I want to loop through all the ActiveX command buttons in a file and change their names.  The first button needs to be named CB1, the second needs to be CB2, etc.

Please help
0
Colelspg
Asked:
Colelspg
  • 3
  • 2
1 Solution
 
GrahamSkanRetiredCommented:
That's probably because there is already a control of that name later in the document.

This will work if it is a Command Button. Otherwise things might get a bit complicated.

Sub ButtonNames()
    Dim ilsh As InlineShape
    Dim i As Integer
    For Each ilsh In ActiveDocument.InlineShapes
        If ilsh.OLEFormat.ClassType = "Forms.CommandButton.1" Then
            i = i + 1
            ilsh.OLEFormat.Object.Name = "TempCB" & i
        End If
    Next ilsh
    i = 0
    For Each ilsh In ActiveDocument.InlineShapes
        If ilsh.OLEFormat.ClassType = "Forms.CommandButton.1" Then
            i = i + 1
            ilsh.OLEFormat.Object.Name = "CB" & i
        End If
    Next ilsh

End Sub


0
 
ColelspgAuthor Commented:
Your hypothesis about a control with the same name is right on.  
I've created several demos of this document and your new code works on all of them except for one.  The document in question is the most complex, with tracked changes and some fancy formatting.  When I attempt to call the ButtonNames macro, I get the following error:
Run-time error '91'
Object Variable or With block variable not set

Any ideas why it would work so well on all but one?
0
 
GrahamSkanRetiredCommented:
I guess that you have some inline shapes that don't have an OLEformat. This code checks the inlineshape type to make sure that it is a control before going on to do the other tests.

Sub ButtonNames()
    Dim ilsh As InlineShape
    Dim i As Integer
    For Each ilsh In ActiveDocument.InlineShapes
        If ilsh.Type = wdInlineShapeOLEControlObject Then
            If ilsh.OLEFormat.ClassType = "Forms.CommandButton.1" Then
                i = i + 1
                ilsh.OLEFormat.Object.Name = "TempCB" & i
            End If
        End If
    Next ilsh
    i = 0
    For Each ilsh In ActiveDocument.InlineShapes
        If ilsh.Type = wdInlineShapeOLEControlObject Then
            If ilsh.OLEFormat.ClassType = "Forms.CommandButton.1" Then
                i = i + 1
                ilsh.OLEFormat.Object.Name = "CB" & i
            End If
        End If
    Next ilsh

End Sub

0
 
ColelspgAuthor Commented:
Thanks, it works great
This is awesome stuff
0
 
GrahamSkanRetiredCommented:
Thanks. It's a shame that the documentation for this area isn't better. It is all there, but it's hard to find.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now