Search code examples
excelvbavisual-studiooutlookoutlook-addin

FileDialog .SelectedItems(1) outlook VSTO add-in is not returning file path


I'm trying to create an Outlook VSTO add-in using Visual Basic in Visual Studio 2017, For some reason I needed to pick up file location by clicking a button from ribbon using Excel FileDialog via importing Microsoft.Office.Interop.Excel. I've added the code here, The same pattern of code for VBA macro is working fine within outlook's built-in VBA window, but when I execute this code (modified for VSTO Add-In) in Visual Studio Debugging mode it opens the FileDialog popup and I can select the single file but .SelectedItems(1) is not returning any path in return to output. Your assistance is always highly appreciated. Thanks.

#

VBA Code (Worked within Outlook VBA Window)

Sub SelectFile()
  Const msoFileDialogFilePicker As Long = 3

  Dim xlObj As Excel.Application
  Dim objDialog As Office.FileDialog
  Dim pstPath As String

  Set xlObj = Excel.Application
  Set objDialog = xlObj.FileDialog(msoFileDialogFilePicker)

  With objDialog
    .AllowMultiSelect = False
    .Title = "Select your PST File"
    .ButtonName = "Ok"
    .Show

    If .SelectedItems.Count = 0 Then
        MsgBox ("No file selected.")
    Else            
        pstPath = .SelectedItems(1)
        MsgBox ("You have selected: " & pstPath)
    End If

  End With
  xlObj.Quit()
  Set objDialog = Nothing
End Sub

VBA_Code

VBA_Code

And... VBA_Output
VBA_Output ..

#

VSTO Code: No File Path in output (Using Visual Studio 2017)

Public Sub SelectFile()

    Const msoFileDialogFilePicker As Long = 3
    Dim xlObj As Excel.Application
    Dim objDialog As Office.FileDialog
    Dim pstPath As String

    xlObj = New Excel.Application
    objDialog = xlObj.FileDialog(msoFileDialogFilePicker)

    With objDialog
        .AllowMultiSelect = False
        .Title = "Select your PST File"
        .ButtonName = "Ok"
        .Show()

        If .SelectedItems.Count = 0 Then
            MsgBox("No file selected.")
        Else                
            pstPath = .SelectedItems(1)
            MsgBox("You have selected: " & .SelectedItems(1))
        End If

    End With
    xlObj.Quit()
    objDialog = Nothing
End Sub

VSTO_Code_VisualStudio17 VSTO_Code_VisualStudio17

And... VSTO_Output VSTO_Output


Solution

  • Note: Method 1 does NOT work with Outlook. Just keeping it included if someone lands here looking for a solution in other VSTO apps.

    Method 1 (Excel, Word, PowerPoint only)

    This is due to zero-based indexing in .NET. Though many VSTO collections use 1-based index, SelectedItems is not one of them. Also note that you should never (ever) use new Application() in a VSTO add-in. All VSTO add-in by default get Globals.ThisAddin.Application (or this.Application if you're already in ThisAddin class) that you can use to access current application instance.

    So your original code should be like this:

    Public Sub SelectFile()
      Dim objDialog = Me.Application.FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker)
    
      With objDialog
        .AllowMultiSelect = False
        .Title = "Select your PST File"
        .ButtonName = "OK"
        If .Show() = -1 Then
          MsgBox("You have selected: " & .SelectedItems(0))
        Else
          MsgBox("No file selected.")
        End If
      End With
    End Sub
    

    Method 2 (All VSTO and other WinForms/WPF apps)

    Since FileDialog is not available in Outlook, an alternate way is to use Microsoft.Win32 dialogs for the same purpose. Here is some code from a past project of mine:

    Public Sub SelectFile()
      Dim openDialog As new Microsoft.Win32.OpenFileDialog()
    openDialog.Filter = "Audio files (*.wav, *.mp3, *.m4a)|*.wav;*.mp3;*.m4a"
      If openDialog.ShowDialog().GetValueOrDefault() Then
       Dim NewAudioFilePath = openDialog.FileName
      End If
    End Sub
    

    You must add reference to PresentationFramework library for this to work.