vba powerpoint按名称选择幻灯片

时间:2014-07-30 14:06:17

标签: vba powerpoint powerpoint-vba powerpoint-2010

我正在尝试按名称选择幻灯片。我通过大纲添加了一个标题。下面是不起作用的代码。 "项目Idaho未在幻灯片集合中找到"

ActivePresentation.Slides("Idaho").Select

2 个答案:

答案 0 :(得分:2)

幻灯片的名称和标题占位符中的文字没有任何关系。

除非您已将其重命名,否则演示文稿中的第一张幻灯片将命名为" Slide1",第二张" Slide2"等等。

如果您特别需要一种方法来找到标题文字=" Idaho"的幻灯片,您需要编写一个功能来搜索演示文稿中的所有幻灯片并返回第一个幻灯片发现符合您的标准。例如:

Sub TestMe()
    Dim oSl As Slide
    Set oSl = FindSlideByTitle("idaho")

    If Not oSl Is Nothing Then
        MsgBox "Found your title on slide " & CStr(oSl.SlideIndex)
    End If

End Sub
Function FindSlideByTitle(sTextToFind As String) As Slide
    Dim oSl As Slide

    For Each oSl In ActivePresentation.Slides
        With oSl.Shapes.Title.TextFrame
            If .HasText Then
                If UCase(.TextRange.Text) = UCase(sTextToFind) Then
                    Set FindSlideByTitle = oSl
                End If
            End If
        End With
    Next

End Function

答案 1 :(得分:0)

复兴一个旧问题,但我想把它扔进去。

尽管ActivePresentation.Slides("MySlideName").Select可能无法正常工作,但这在PPT 2010中对我有用:

Dim PPTObj As PowerPoint.Application
Set PPTObj = New PowerPoint.Application
Dim PPTClinic As PowerPoint.Presentation
Set PPTClinic = PPTObj.Presentations.Open(FileName:="Your File Name Here")
PPTClinic.Slides("MySlideName").Select

当然,这假设有一张名为" MySlideName"的幻灯片。您的代码必须妥善处理Item MySlideName not found in the Slides collection.错误(err.number = -2147188160)。