AW: Datei auswählen & anhängen
14.05.2007 13:14:00
Webtramp
Hi Christian,
anbei der Code zum Auswählen einer Datei, habe ich etwas angepasst, stammt ursprünglich von Microsoft:
Du kannst der Funktion die Überschrift für den Dialog mitgeben und mit Multiselect bestimmen, ob nur eine oder mehrere Dateien ausgewählt werden dürfen.
Als Rückgabewert erhälst du den Pfad zur Datei.
Function SelectFile(ByVal strDialogTitle As String, ByVal bMultiSelect As Boolean) As String
'Declare a variable as a FileDialog object.
Dim fd As FileDialog
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
'Allow the user to select multiple files.
.AllowMultiSelect = False
.Title = strDialogTitle
.ButtonName = "Öffnen"
.AllowMultiSelect = bMultiSelect
.Filters.Clear
.Filters.Add "Nur Exceldateien", "*.xls", 1
'Use the Show method to display the File Picker dialog box and return the user's action. _
'If the user presses the action button...
If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is a String that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example simply displays the path in a message box.
If bMultiSelect Then
'Does SelectFile contain at least one path than attach next one by semicolon
If Len(SelectFile) > 0 Then
SelectFile = SelectFile & ";" & vrtSelectedItem
Else
SelectFile = vrtSelectedItem
End If
Else
SelectFile = vrtSelectedItem
End If
Next
'If the user presses Cancel...
Else
SelectFile = vbNullString
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Function