[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Code to Change Command Button Name

Posted on 2007-07-23
5
Medium Priority
?
737 Views
Last Modified: 2008-01-09
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
Comment
Question by:Colelspg
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 19551753
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
 

Author Comment

by:Colelspg
ID: 19552170
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 19552572
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
 

Author Comment

by:Colelspg
ID: 19556971
Thanks, it works great
This is awesome stuff
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 19559282
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

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
With its various features, Office 365 can not only help you with your day-to-day business tasks, it can also do wonders for your marketing campaign.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question