vba 通过 Id 或 Name 获取形状
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5527073/
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
Get shape by Id or Name
提问by Mateen Ulhaq
Is there any way to get a shape if you know its Id
?
如果你知道它,有没有办法得到一个形状Id
?
For example:
例如:
Dim myshape As Shape
myshape.Id = 42
myshape = getShapeById(myshape.Id)
Or, alternatively, could I get the shape by Name
?
或者,或者,我可以通过 获得形状Name
吗?
Dim myshape As Shape
myshape.Name = "Rectangle 42"
myshape = getShapeByName(myshape.Name)
采纳答案by Mateen Ulhaq
To get a Shape
by Name
, you do...:
要获得Shape
通过Name
,您可以...:
Function getShapeByName(shapeName As String, Slide As Integer)
Set getShapeByName = ActivePresentation.Slides(Slide).Shapes(shapeName)
End Function
Dim myshape As Shape
myshape = getShapeByName("Rectangle 42", 1)
回答by Todd Main
Getting a shape .Name
by its .Id
is somewhat more convoluted than getting its .Id
by its .Name
.
入门的形状.Name
由它.Id
在一定程度上超过费解得到它.Id
通过它的.Name
。
But here's how it's done:
但这是如何完成的:
Sub PrintShapeName()
Debug.Print getNameByID(3, 1)
End Sub
Function getNameByID(shapeID As Long, slide As Integer)
Dim ap As Presentation: Set ap = ActivePresentation
Dim sl As slide: Set sl = ap.Slides(slide)
sl.Shapes.SelectAll
Dim sr As ShapeRange
Set sr = Windows(1).Selection.ShapeRange
Dim s As Shape
For Each s In sr
If s.id = shapeID Then
getNameByID = s.Name
Exit Function
End If
Next
End Function
回答by chris neilsen
Not sure about by ID, but by name use the sheet Shapes collection object
不确定按 ID,但按名称使用工作表 Shapes 集合对象
Set myShape = <SheetObject>.Shapes("<ShapeName>")
eg
例如
Set myShape = ActiveSheet.Shapes("Rectangle 1")
回答by Chris Christiansson
sName = ActivePresentation.Slides(k).Shapes(j).Name
where k
is the slide number and j
and the shape number on that slide.
k
该幻灯片上的幻灯片编号和j
形状编号在哪里。
You can loop through each pages shapes with something like:
您可以使用以下内容遍历每个页面形状:
k = 1
For j = 1 To ActivePresentation.Slides(k).Shapes.Count
Next j
Chris
克里斯