在Powrpoint演示文稿中选择幻灯片的输入框。 (即将完成)

时间:2019-06-14 17:18:54

标签: arrays vba select powerpoint-vba inputbox

你好,所有聪明的人都在那里,

我想根据输入框的输入选择一些Powerpointslides,但我无法使其正常工作。我如何声明变量可能有问题。我创建了一个名为Powerpointslides的宏,我想在VBA的帮助下通过输入框选择幻灯片的名称。

所以基本上,我希望输入框返回一个幻灯片名称数组。假设:如果在输入框中输入表格,我想选择一个名为USA和Sweden的表格。到目前为止,这是我尝试过的。

Sub Select_Slides()


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

list = Array(slides)

ActivePresentation.Slides.Range(list).Select

End Sub

要使其工作,清单必须是名为USA和Sweden的工作表的Array。我有一个宏,它仅使用选定的幻灯片创建一个新的Powerpoint。所以这就是为什么我要通过输入框选择幻灯片。

谢谢

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

希望这会有所帮助!

答案 2 :(得分:0)

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