Search code examples
vbapowerpoint

How to import selected presentations from a folder?


How can I import a selected presentation from a particular folder?

Below are the code which I tried but it is importing all the presentations which are stored in a particular folder.

Sub Merge()
' After doing the merge, open presentation #1
' Then run this code:

    Dim sPath As String
    Dim cFileNames As New Collection
    Dim sTemp As String
    Dim x As Long

    sPath = CurDir  ' by default
    If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"

    sPath = InputBox("Path to PPT files (ex: c:\my documents\", _
        "Where are the files?", sPath)
    If sPath = "" Then
        Exit Sub
    End If

    sTemp = Dir(sPath & "*.pptx")
    While sTemp <> ""
        With cFileNames
            .Add (sPath & sTemp)
        End With
        sTemp = Dir
    Wend

    If cFileNames.Count > 1 Then
        ' open the first file
        Presentations.Open (cFileNames(1))

        ' Insert the other files
        For x = 2 To cFileNames.Count
            Call ActivePresentation.Slides.InsertFromFile( _
                cFileNames(x), _
                ActivePresentation.Slides.Count)
        Next
    End If

End Sub

For example. I have 10 presentations in XYZ folder but want to import only four selected presentations.


Solution

  • This should do it:

    Sub Merge()
        Dim sPath As String
        Dim dlgOpen As FileDialog
        Dim x As Integer
        Dim vrtSelectedItem As Variant
        
        'Set path to current directory
        sPath = CurDir
        If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
        
        'Set File Picker dialog
        Set dlgOpen = Application.FileDialog(Type:=msoFileDialogFilePicker)
        
        'Set initial path and view, set multiselect capability
        With dlgOpen
            .InitialFileName = sPath
            .InitialView = msoFileDialogViewList
            .AllowMultiSelect = True
            
            'If user click on OK, insert selected files after last slide of current presentation
            If .Show = -1 And .SelectedItems.Count > 0 Then
                For x = 1 To .SelectedItems.Count
                    ActivePresentation.Slides.InsertFromFile .SelectedItems(x), ActivePresentation.Slides.Count
                Next
            Else
                'User Cancelled
            End If
        End With
    End Sub