Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defaulting a folder for FileDialog in VBA

Tags:

vba

filedialog

Private Sub Command93_Click()      Dim f As Object     Dim strFile As String     Dim strFolder As String     Dim varItem As Variant     Dim P As String     Dim DeleteEverything As String          DoCmd.SetWarnings False         DeleteEverything = "DELETE * FROM [TABLE]"         DoCmd.RunSQL DeleteEverything      Set f = Application.FileDialog(3)     f.AllowMultiSelect = False     If f.Show Then         For Each varItem In f.SelectedItems             strFile = Dir(varItem)             strFolder = Left(varItem, Len(varItem) - Len(strFile))             P = strFolder & strFile         Next     End If     Set f = Nothing          DoCmd.TransferText acImportFixed, "[IMPORT SPECIFICATION]", "[TABLE]", P, False  End Sub 

How to make the FileDialog default to a specific folder when it initially opens?

like image 708
Chuck Avatar asked Jun 04 '13 11:06

Chuck


People also ask

How do I change the path in VBA?

“ChDir” can be termed as “Change Directory.” By using “ChDir,” we can change the current default directory used in VBA when searching for the files without a fully qualified path.

Can Excel VBA create a folder?

The Microsoft Excel MKDIR statement allows you to create a new folder or directory. The MKDIR function is a built-in function in Excel that is categorized as a File/Directory Function. It can be used as a VBA function (VBA) in Excel.


1 Answers

Add the folder path (including the trailing \) to InitialFileName. For example, to open the dialog in the user's home directory do:

f.InitialFileName = Environ("USERPROFILE") & "\" 

If you forget the trailing \, then the dialog box will still open in the correct folder, but the folder name will also appear as the default selected file name. Then the dialog will be looking for a sub-folder with the same name, which usually doesn't exist.

like image 151
Chuck Avatar answered Sep 17 '22 13:09

Chuck