簡體   English   中英

如何在 PowerPoint VBA 中通過相似的名稱選擇多個形狀?

[英]How to select multiple shapes by similar name in PowerPoint VBA?

有沒有辦法從具有相同形狀名稱的幻燈片中選擇多個形狀。

例如,我有 5 個形狀,名稱為“Textbox 60”。 我想運行一個宏,從名為“Textbox 60”的幻燈片中選擇所有形狀。 使用了下面的代碼。

ActiveWindow.View.Slide.Shapes.Range("Textbox 60").Select

這是一種方法:

Sub Tester()
    SelectByName ActivePresentation.Slides(1), "Textbox1"
End Sub

Sub SelectByName(sld As Slide, nm As String)
    Dim s As Shape, first As Boolean
    first = True
    For Each s In sld.Shapes
        If s.Name = nm Then
            s.Select first 'Argument determines whether to add to 
            first = False  '  existing selection, or replace it
        End If
    Next s
End Sub

不過,您應該嘗試遵循@TinMan 的建議 - 這是“更好”的方法。

應盡可能避免激活和選擇對象。 您最好使用 ShapeRange 處理形狀。

Sub Main()
    Dim ShapeRange As ShapeRange
    Set ShapeRange = FindShapes(ActiveWindow.View.Slide, "Textbox 60")
    If Not ShapeRange Is Nothing Then
        
    End If
End Sub


Function FindShapes(Slide As Slide, Pattern As String) As ShapeRange
    Dim Results() As Long
    ReDim Results(1 To Slide.Shapes.Count)
    Dim n As Long
    Dim Index As Long
    For Index = 1 To Slide.Shapes.Count
        With Slide.Shapes(Index)
            .Name = "Textbox 60"
            If .Name Like Pattern Then
                n = n + 1
                Results(n) = Index
            End If
        End With
    Next
    
    If n > 0 Then
        ReDim Preserve Results(1 To n)
        Set FindShapes = Slide.Shapes.Range(Results)
    End If
End Function

注意:我重寫了代碼來處理多個同名形狀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM