如何在Excel中获取Shape的内部名称
当使用 Shapes.AddPicture(...) 方法将图片插入到 Excel 工作表中时,Excel 会自动为其指定名称“图片 1”、“图片 2”等。
此名称可用于获取 Shapes 集合中此形状对象的引用,如 Shapes.Item("Picture 1")。如果在 Excel 中使用名称框更改名称,则可以通过两个不同的名称(或其中之一是键/标题)来引用 Shape 对象。因此,如果我将名称更改为“MyPic”,我可以使用其中任何一个来引用 Shapes 集合中的形状:
Shapes.Item("Picture 1")
OR
Shapes.Item("MyPic")
可以使用 VBA 中的 Shape.Name 属性来访问该名称,但我们如何访问不支持的其他值 (MyPic)内部似乎发生了变化?
已更新
我想做的是将单元格链接到 Excel 中的图片。我将图片数据保存在单元格的注释中。这些是场景:
- 如果我保留图片名称(外部名称),则将结构复制粘贴到同一工作表上将重复名称,并且单元格将指向相同的结构。
- 如果我保留内部名称,则复制粘贴到其他工作表将产生问题,因为同一工作簿中的某些其他工作表上可能存在相同的内部名称。
- 如果我获取 ID,我将无法从中获取图片参考
对我来说,获取内部名称很重要。我有形状参考,但不知道如何从此参考中获取内部名称。
When a picture is inserted into an Excel worksheet using Shapes.AddPicture(...) method, Excel gives it a name "Picture 1", "Picture 2" etc automatically.
This name can be used to get a reference to this shape object in Shapes collection like Shapes.Item("Picture 1"). If the name is changed in Excel using the Name Box, there are two different names (or one of them is a key/Caption) through which the Shape object can be referenced. So if I change the name to "MyPic" I can use any of these to reference a shape in Shapes collection:
Shapes.Item("Picture 1")
OR
Shapes.Item("MyPic")
The name can be accessed using Shape.Name property in VBA but how can we access the other value (MyPic) that does not seem to change internally?
UPDATED
What I am trying to do is to link a cell to a picture in Excel. I keep the picture data in cell's comment. These are the scenarios:
- If I keep the picture name (external name) then copy-pasting the structure on the same worksheet will duplicate the name and the cell will point to the same structure.
- If I keep the internal name then copy-pasting to other worksheet will create problem as the same internal name can exist on some other worksheet in the same workbook.
- If I take ID, I will not be able to get the Picture reference from it
For me getting the internal name is important. I have the Shape reference but no idea how to get the internal name from this ref.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将形状添加到工作表 (oSht) 后,您可以立即使用
oSht.Shapes(Osht.Shapes.count)
来引用它。因此,
oSht.Shapes(osht.shapes.count).Name
将为您提供其名称。如果您想在 Shapes 集合中查找形状的索引并且知道其名称,那么您需要循环
Shapes.Name
直到找到它。如果您知道索引,则可以构造“Picture n”备用名称,或者可以存储“Picture n”备用名称。您还可以存储形状的 ID 属性,然后通过循环遍历 Shapes 集合来引用该形状,直到找到Shape.ID
如果用户将形状移动到另一个工作表,然后重命名它,无法将其识别为原始形状,因为外部名称、备用名称、形状索引和 ID 都会不同。因此,如果这是您的情况中的问题,您需要考虑卷影复制或工作表保护。
Immediately after you have added the shape to a worksheet (oSht) you can use
oSht.Shapes(Osht.Shapes.count)
to reference it.So,
oSht.Shapes(osht.shapes.count).Name
will give you its name.If you want to find the index of a shape in the Shapes collection and you know its name then you need to loop through
Shapes.Name
until you find it. If you know the Index, then you can construct the "Picture n" alternate name, or you can store the "Picture n" alternate name. You can also store the ID property of the shape and then reference the shape by looping through the Shapes collections until you find theShape.ID
If the user moves the shape to a different sheet and then renames it, there is no way of identifying it as the original shape, because the external name, alternate name, Shapes index, and ID will all be different. So, if this is a problem in your scenario you would need to consider shadow copying or sheet protection.