[VBA] GetFolder / GetFile

Gepostet am: Nov 10, 2013 8:51:9 AM

Oftmals braucht man zum Speichern oder Öffnen einen Datei/Folder-Selector. Mit Office XP wurde dazu das FileDialog-Objekt eingeführt.

Hauptelement ist folgender Bestandteil

> Dim fldr As FileDialog

>  Set fldr = Application.FileDialog(XXYY)

Zur Auswahl stehen: (MsoFileDialogType)

Die zwei wichtigsten hierbei sind der Zugriff auf Ordner-Pfade (GetFolder) und der Zugriff auf Datei-Pfade (GetFile)

GetFolder

GetFolder

Public Function GetFolder(strPath As String) As String     Dim fldr As FileDialog     Dim sItem As String     Set fldr = Application.FileDialog(msoFileDialogFolderPicker)     With fldr         .Title = "Select a Folder"         .AllowMultiSelect = False         .InitialFileName = strPath         If .Show <> -1 Then GoTo NextCode         sItem = .SelectedItems(1)     End WithNextCode:     GetFolder = sItem     Set fldr = NothingEnd Function

GetFile

GetFile

Public Function GetFile(strPath As String) As String     Dim filedr As FileDialog     Dim pickedfile As Boolean     Dim myfile As String     Set filedr = Application.FileDialog(msoFileDialogFilePicker)     With filedr         .InitialFileName = strPath         .AllowMultiSelect = False         .Title = "Select File"         .Filters.Clear         .Filters.Add "Picture Files", "*.jpg"         .Filters.Add "All Files", "*.*"         pickedfile = False         pickedfile = .Show         If pickedfile Then         myfile = .SelectedItems.Item(1)         End If     End WithEnd Function