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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:18:59  来源:igfitidea点击:

Get shape by Id or Name

vbapowerpointpowerpoint-vba

提问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 Shapeby 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 .Nameby its .Idis somewhat more convoluted than getting its .Idby 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 kis the slide number and jand 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

克里斯