通过文本框选择已定义的幻灯片范围

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过文本框选择已定义的幻灯片范围相关的知识,希望对你有一定的参考价值。

我想通过vba在Powerpoint中选择一系列幻灯片。对于Excel,您只需编写

范围( “A1:A100”)

我想从幻灯片编号5到10中选择,我该如何编码?我的代码只选择了两张幻灯片:

ActivePresentation.Slides.Range(阵列(5,10))。选择

答案

这是一个例子。这取决于您使用的文本框类型。我假设你选择的标准文本框是一个形状。我正在使用一些测试文本框,您需要根据自己的需要进行调整。

您调用子SelectSlides以生成幻灯片的数组数组,以便在两个文本框号之间进行选择。

您可以使用Range方法返回任意数量的形状或幻灯片。要为Index指定整数或字符串数​​组,可以使用Array函数,或者我相信,将数组传递给Range。

我会提前,虽然这工作一次,powerpoint崩溃,从那时起代码只选择了最后一项。

但是,您可以将ppt.Slides.Range(slidesArray)分配给Slides对象并使用该对象进行处理。

Dim slidesObject As Slides

Set slidesObject = ppt.Slides.Range(slidesArray)

码:

Option Explicit

Public Sub SelectSlideArray()

    Dim ppt As Presentation
    Dim sld As Slide
    Dim textBox1 As Shape
    Dim textBox2 As Shape

    Set ppt = ActivePresentation
    Set sld = ppt.Slides(1) ' slide with text boxes in 
    Set textBox1 = sld.Shapes("TextBox 3") 'change as required
    Set textBox2 = sld.Shapes("TextBox 4") 'change as required

    textBox1.TextFrame.TextRange = 5 ' you can say TextRange.Text but .Text is defaut
    textBox2.TextFrame.TextRange = 10

    Dim startSlideNumber As Integer
    Dim endSlideNumber As Integer

    startSlideNumber = Int(textBox1.TextFrame.TextRange)
    endSlideNumber = Int(textBox2.TextFrame.TextRange)

    SelectSlides ppt, startSlideNumber, endSlideNumber

    'PrintShapeNames sld

End Sub

Public Sub SelectSlides(ByVal ppt As Presentation, ByVal startSlideNumber As Long, ByVal endSlideNumber As Long)

    Dim outputSlideNumber As Long

    outputSlideNumber = startSlideNumber

    If ppt.Slides.Count < endSlideNumber Then
        MsgBox "You don't have enough slides in the presentation!"
        End
    ElseIf endSlideNumber < startSlideNumber Then
        MsgBox "End slide is before start slide!"
        End
    Else
       Dim slidesArray()
       ReDim slidesArray(0 To endSlideNumber - startSlideNumber)
       Dim currentSlide As Long

       For currentSlide = LBound(slidesArray) To UBound(slidesArray)

           slidesArray(currentSlide) = outputSlideNumber
           outputSlideNumber = outputSlideNumber + 1

       Next currentSlide

    End If

    ppt.Slides.Range(slidesArray).Select

End Sub

Private Sub PrintShapeNames(ByVal sld As Slide)

    Dim shp As Shape

    For Each shp In sld.Shapes
        Debug.Print shp.Name  
    Next shp

End Sub

以上是关于通过文本框选择已定义的幻灯片范围的主要内容,如果未能解决你的问题,请参考以下文章

Powerpoint VBA 选择幻灯片特定区域内的所有形状

winform文本框超出

for next 语句中的“整数超出范围”错误

通过消除除 seacrhed 行之外的所有其他行,通过文本框在已填充的列表视图中搜索

ppt中如何对文字设置阴影效果

通过所有循环vba保持选择