如何在Excel中获取Shape的内部名称 [英] How to get Shape's internal name in Excel

查看:961
本文介绍了如何在Excel中获取Shape的内部名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用Shapes.AddPicture(...)方法将图片插入到Excel工作表中时,Excel会自动为其添加名称Picture 1,Picture 2等。

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集合中获取对此形状对象的引用,如Shapes.Item(Picture 1)。如果使用名称框在Excel中更改了名称,则可以使用两个不同的名称(或其中一个是键/ Caption),可以通过该名称引用Shape对象。所以如果我将名称改为MyPic,我可以使用其中任何一个引用Shapes集合中的一个形状:

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")

可以使用VBA中的Shape.Name属性访问该名称,但是如何访问内部似乎没有更改的其他值(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

我想要做的是将单元格链接到Excel中的图片。我保留图片数据在单元格的评论。这些是以下情况:

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:


  1. 如果我保留图片名称(外部名称),则复制粘贴同一工作表上的结构将复制名称和单元格将指向相同的结构。

  2. 如果我保留内部名称,则复制粘贴到其他工作表将创建问题,因为在其他工作表上可能存在相同的内部名称相同的工作簿。

  3. 如果我取得ID,我将无法从中获取图片参考

对于我获取内部名称很重要。我有Shape参考,但不知道如何从这个参考文件中获取内部名称。

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.

推荐答案

添加形状后立即到一个工作表(oSht),你可以使用 oSht.Shapes(Osht.Shapes.count)引用它。
所以, oSht.Shapes(osht.shapes.count).Name 会给你的名字。

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.

如果要在Shapes集合中找到形状的索引,并且知道其名称,那么您需要循环使用 Shapes.Name 直到找到它。如果您知道索引,那么可以构造图片n备用名称,也可以存储图片n备用名称。您还可以存储形状的ID属性,然后循环遍历Shapes集合引用形状,直到找到 Shape.ID

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 the Shape.ID

如果用户将形状移动到其他工作表,然后将其重命名,则无法将其标识为原始形状,因为外部名称,备用名称,形状索引和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.

这篇关于如何在Excel中获取Shape的内部名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆