Powrpoint 演示文稿中 select 幻灯片的输入框。 (几乎完成)

Inputbox to select slides in Powrpoint presentation. (Almost done)

你好所有聪明的人,

我想 select 一些基于输入框输入的 Powerpoint 幻灯片,但我无法让它工作。我声明变量的方式可能有问题。我创建了一个名为 Powerpointslides 的宏,我想在 VBA 的帮助下 select 输入框的幻灯片名称。

所以基本上我希望输入框 return 一组幻灯片名称。比方说:如果我在输入框中输入它,我想 select 一个叫做 USA 和 Sweden 的 sheet。这是我到目前为止尝试过的。

Sub Select_Slides()


slides = InputBox("Insert Slide names to select")

list = Array(slides)

ActivePresentation.Slides.Range(list).Select

End Sub

为了让它工作,列表必须是一个名为 USA 和 Sweden 的 sheet 数组。我有一个宏可以创建一个只有 selected 幻灯片的新 Powerpoint。所以这就是为什么我想通过输入框 select 幻灯片。

谢谢

首先,您需要格式化 InputBox 将要 return 的字符串。我编写了这个名为 CreateCorrectArray 的函数,它将从您的 slides 字符串中获取幻灯片的名称,而不是逗号分隔符。例如,如果要 select 名为 "Slide1" 和 "Slide4" 的幻灯片,则需要在 InputBox 中输入 "Slide1,Slide4",因此该函数将 return 一个数组("Slide1", "Slide4").

Sub Select_Slides()

    slides = InputBox("Insert Slide names to select")

    list = CreateCorrectArray(slides)

    ActivePresentation.slides.Range(list).Select

End Sub

'' Create the array from string whereas comma separator
Function CreateCorrectArray(ByVal slides As String) As String()
    Dim indexChar As Integer
    Dim indexAr As Integer
    Dim startOfSlideName As Integer
    Dim MyArray() As String
    Dim nSlides As Integer

    '' Number of slides
    nSlides = ActivePresentation.slides.Count

    '' Array that storage the slides names
    ReDim MyArray(nSlides)


    indexAr = 1
    startOfSlideName = 1 '' start of slide name in the string "slides"

    '' Loop trough each character in "slide" string
    For indexChar = 1 To Len(slides)

        '' if the character is a comma
        If Mid(slides, indexChar, 1) = "," Then

           '' storage the slide's name in the array
           MyArray(indexAr) = Mid(slides, startOfSlideName, indexChar - startOfSlideName)

           indexAr = indexAr + 1
           startOfSlideName = indexChar + 1
        End If

        '' At the end of slides string, there will be
        '' no comma, so for this case, add the last
        '' slide name in MyArray
        If indexChar = Len(slides) Then
            MyArray(indexAr) = Mid(slides, startOfSlideName)
        End If
    Next

    CreateCorrectArray = MyArray
End Function

以下宏将提示用户列出一个或多个要选择的幻灯片名称,并用 semi-colon 分隔,它还包括一些错误处理。

Sub Select_Slides()

    Dim slideNames As String
    Dim slideNameArray As Variant
    Dim selectedSlideRange As slideRange
    Dim i As Long

    'prompt user to list slide names using a semi-colon as a separator
    slideNames = InputBox("Insert slide names to select using a semi-colon as a separator.")

    'if inputbox is empty, or user cancelled, exit sub
    If Len(slideNames) = 0 Then
        MsgBox "Inputbox is either empty, or user cancelled!", vbExclamation
        Exit Sub
    End If

    'split the names into an array
    slideNameArray = Split(slideNames, ";")

    'remove any leading or trailing spaces
    For i = LBound(slideNameArray) To UBound(slideNameArray)
        slideNameArray(i) = Trim(slideNameArray(i))
    Next i

    'assign the selected slides to a slide range
    On Error Resume Next
    Set selectedSlideRange = ActivePresentation.Slides.Range(slideNameArray)
    On Error GoTo 0

    If selectedSlideRange Is Nothing Then
        MsgBox "One or more listed slides not found!", vbExclamation
    Else
        selectedSlideRange.Select
    End If

    Set selectedSlideRange = Nothing

End Sub

希望对您有所帮助!

提醒一下:当您需要将分隔字符串转换为数组时,Split 方法可以完成大部分繁重的工作。

Sub SplitExample()

    Dim sText As String
    ' This would be your InputBox results, but for demo purposes:
    Dim aInputarray() As String
    Dim x As Long

    sText = "USA,Sweden"

    ' Split takes the text to split and the delimiter as parameters
    ' and returns a 0-based array
    aInputarray = Split(sText, ",")

    For x = LBound(aInputarray) To UBound(aInputarray)
        Debug.Print aInputarray(x)
    Next

End Sub