vba 如何在Excel中获取Shape的内部名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3744986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Shape's internal name in Excel
提问by A9S6
When a picture is inserted into an Excel worksheet using Shapes.AddPicture(...) method, Excel gives it a name "Picture 1", "Picture 2" etc automatically.
当使用 Shapes.AddPicture(...) 方法将图片插入 Excel 工作表时,Excel 会自动为其命名为“图片 1”、“图片 2”等。
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 集合中此形状对象的引用,如 Shapes.Item("Picture 1")。如果使用名称框在 Excel 中更改名称,则可以通过两个不同的名称(或其中一个是键/标题)来引用 Shape 对象。因此,如果我将名称更改为“MyPic”,我可以使用其中任何一个来引用 Shapes 集合中的形状:
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?
可以使用 VBA 中的 Shape.Name 属性访问该名称,但是我们如何访问似乎在内部没有更改的其他值 (MyPic)?
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:
更新
我想要做的是将单元格链接到 Excel 中的图片。我将图片数据保留在单元格的评论中。这些是场景:
- 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.
对我来说,获得内部名称很重要。我有 Shape 参考,但不知道如何从这个参考中获取内部名称。
回答by Charles Williams
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).Namewill give you its name.
将形状添加到工作表 (oSht) 后,您可以立即使用oSht.Shapes(Osht.Shapes.count)它来引用它。所以,oSht.Shapes(osht.shapes.count).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.Nameuntil 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 the Shape.ID
如果你想在 Shapes 集合中找到一个形状的索引并且你知道它的名字,那么你需要循环Shapes.Name直到找到它。如果您知道索引,那么您可以构造“Picture n”备用名称,或者您可以存储“Picture n”备用名称。您还可以存储形状的 ID 属性,然后通过遍历 Shapes 集合来引用该形状,直到找到Shape.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.
如果用户将形状移动到不同的工作表,然后对其重命名,则无法将其识别为原始形状,因为外部名称、备用名称、形状索引和 ID 都将不同。因此,如果这是您的方案中的一个问题,您将需要考虑卷影复印或纸张保护。

