用于在 Powrpoint 演示文稿中选择幻灯片的输入框.(快完成了)



你好,所有聪明的窥视者,

我想根据输入

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

所以基本上我希望输入框返回幻灯片名称数组。假设:如果我在输入框中输入名为"美国和瑞典"的工作表,我想选择它。这就是我到目前为止尝试过的。

Sub Select_Slides()

slides = InputBox("Insert Slide names to select")
list = Array(slides)
ActivePresentation.Slides.Range(list).Select
End Sub

为了使它工作列表确实是一个名为美国和瑞典的工作表的数组。我有一个宏,它创建一个仅包含选定幻灯片的新Powerpoint。所以这就是为什么我想通过输入框选择幻灯片。

谢谢

提醒一下:当您需要将分隔字符串转换为数组时,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

首先,您需要格式化InputBox将返回的字符串。我编写了一个名为 CreateCorrectArray 的函数,它将从您的slides字符串中获取幻灯片的名称,而使用逗号分隔符。例如,如果要选择名为"Slide1"和"Slide4"的幻灯片,则需要在输入框中输入"Slide1,Slide4",因此该函数将返回一个Array("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

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

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

希望这有帮助!

最新更新