The Application.FileDialog family of dialog boxes works fine for most Microsoft Office products, but will fail with run-time error 438 when issuing the command from Microsoft Outlook VBA. In other words, there is no FileDialog property under the Application object in Outlook VBA.
Outlook Runtime Error 438 Application FileDialog
In this tutorial, I’m going to show you how to still use the same dialog prompts in Outlook without receiving this error.
Tutorial - Outlook FileDialog
To use the FileDialog suite of dialog boxes from Outlook, you’ll have to create a reference to the Microsoft Excel or Microsoft Word object library. I’ll use the Excel Object Library in this example.
- From your Outlook VBA Editor, go to Tools > References
- Scroll down and check the box beside Microsoft Excel XX.0 Object Library, where XX is an integer, like 14.
Add Reference to Excel Object Library
Once this is done, copy and paste the following macro in your Outlook Module.
Sub SelectFolder()
Dim sFolder As String
Dim xlObj As Excel.Application
Set xlObj = New Excel.Application
' Open the select folder prompt
With xlObj.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then ' if OK is pressed
sFolder = .SelectedItems(1)
End If
End With
xlObj.Quit
Set xlObj = Nothing
If sFolder <> "" Then ' if a file was chosen
' *********************
' put your code in here
' *********************
End If
End Sub
Make powerful macros with our free VBA Developer Kit Tutorials like this can be complicated. That’s why we created our free VBA Developer Kit and our Big Book of Excel VBA Macros to supplement this tutorial. Grab them below and you’ll be writing powerful macros in no time.
This macro opens the xlObj.FileDialog
:
- msoFileDialogOpen - Display an Open Dialog Box
- msoFileDialogSaveAs - Display a Save As Dialog Box
- msoFileDialogFilePicker - Display a File Picker Dialog Box
- msoFileDialogFolderPicker - Display a Folder Picker Dialog Box
Here’s what the Folder Picker looks like from Outlook:
VBA msoFileDialogFolderPicker Prompt
Notice the little Excel logo in the upper left of the FileDialog prompt. That’s there because we’re calling the Excel version of the FialDialog:)
That’s all for this tutorial. When you’re ready to take your VBA to the next level, subscribe using the form below and follow me on Twitter.