Search code examples
vbapowerpoint

Powerpoint FileDialog box issues (VBA)


In a small Powerpoint application I'm coding I use the .FileDialog method to enable the user to select the target file for the app. Everything works fine, except if the user wants to cancel the dialog by either clicking the cancel button or the X in the upper RH corner, an error is generated and execution fails.

So, what are the PowerPoint error traps if the user wants to cancel? I tried using Excel VBA code ('On Error', vbCancel, and If statements) to trap the error with no luck.

Any suggestions?

Sub ShowFileDialog()

Dim dlgOpen As FileDialog

Set dlgOpen = Application.FileDialog(Type:=msoFileDialogOpen)

With dlgOpen
    .AllowMultiSelect = True
    .Show

[meta code] If selection = "" then exit sub
or
if vbCancel = True then exit sub

End With

End Sub

Solution

  • Show returns a value.

    Sub ShowFileDialog()
    Dim dlgOpen As FileDialog`
    
    Set dlgOpen = Application.FileDialog(Type:=msoFileDialogOpen)
    
    With dlgOpen
        .AllowMultiSelect = True
        If .Show Then
           Dim I As Integer
           For I = 1 To .SelectedItems.Count
              Debug.Print .SelectedItems(I)
           Next
        Else
           Debug.Print "User cancelled"
        End If
     End With
    
    End Sub