如何在没有Application.Caller的情况下获得单击的形状

问题描述:

如何在不使用 Application.Caller 的情况下检索单击以运行宏的形状?它的名称或形状本身就是一个对象.

How can I retrieve the shape that was clicked to run a macro, without using Application.Caller?. Its name or the shape itself as an object.

我不能使用 Application.Caller ,因为如果形状名称大于30个字符,它将仅检索前30个字符.

I cant use Application.Caller because if the shape name is bigger than 30 characters, it retrieves only the first 30 characters.

也许有一个可以检索Sape对象的API?

Maybe there is an API that could retrieve the Sape object?

  1. 我不想在工作表中循环显示形状.在工作簿中循环遍历所有形状会花费太多时间.
  2. 我无法重命名形状.

谢谢

可以重命名工作表中的Shape对象吗?

Can you rename the Shape objects in your Worksheet?

Private Sub Workbook_Open()
Dim shp As shape
Dim i As Integer

For Each shp In ThisWorkbook.Sheets(1).Shapes
    shp.Name = "Shape" & Format(i, "0000")
    i = i + 1
Next shp
End Sub

或者,您可以手动设置每个形状的.OnAction属性,以将形状名称传递给函数:

Alternatively, you could manually set the .OnAction property of each shape to pass the shape name to your function:

Sub RenameShapes2()

    Dim shp As shape
    Dim i As Integer

    For Each shp In ThisWorkbook.Sheets(1).Shapes
        shp.name = "Shape" & "abcdefghijklmnopqrstuvwxyz0123456789_" & Format(i, "0000")
        shp.OnAction = "'userFunction """ & shp.name & """'"
        i = i + 1
    Next shp

End Sub


Sub userFunction(name As String)
    MsgBox name
End Sub 

这使您可以拥有长度超过30个字符的Shape名称.虽然有点难看

This allows you to have Shape names over 30 characters in length. It's kinda ugly though