以编程方式重命名activeX文本框“(名称)”字段

我怎样才能以编程方式重命名ActiveX文本框? 我指的是属性窗口中的(名称)字段。

ActiveX文本框可以通过其现有名称或Shapes集合中的序号来引用。

如果您知道文本框当前名为TextBox1,并且您想将其称为TextBox99 ,则直接引用它。

With ActiveSheet '<-reference the worksheet properly! .Shapes("TextBox1").Name = "TextBox99" End With 

如果要重命名工作表中的所有文本框,然后遍历所有形状。

 Dim t As Long, s As Long With ActiveSheet '<-reference the worksheet properly! For s = 1 To .Shapes.Count If CBool(InStr(1, .Shapes(s).Name, "textbox", vbTextCompare)) Then t = t + 1 .Shapes(s).Name = "MyTextBox" & Format(t, "00") End If Next s End With 

请注意.Shapes集合的索引是基于一 ,而不是从零开始 。 根据你要完成的工作,错误控制,以确保你不试图重命名文本框作为已经存在的东西将是谨慎的。

Jeeped的回答对Excel很好,但不幸的是不是Word(我也需要)。 所以在这里也是为了Word:

 Dim shp As InlineShape On Error Resume Next For Each shp In ActiveDocument.InlineShapes If Not shp.OLEFormat Is Nothing And _ shp.OLEFormat.Object.Name = "TextBox1" Then shp.OLEFormat.Object.Name = "TextBox99" End If Next On Error GoTo -1