VBA - Pfad über Fenster auswählen und als String speichern?

3 Antworten

Ja, das geht.

Folgenden Code nutze ich und musst du dir halt noch anpassen. (Texte und die erlaubten Dateiendungen.
Bei Fragen einfach melden.

'TXT-Datei auswählen
Dim varDatei As Variant

With Application.FileDialog(msoFileDialogFilePicker)
.InitialFileName = ThisWorkbook.Path
.Title = "Bitte wählen Sie die gewünschte Projektdaten-TXT-Datei aus."
.ButtonName = "Diese TXT-Datei importieren"
.InitialView = msoFileDialogViewDetails
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "TXT-File", "*.txt", 1

If .Show = -1 Then
varDatei = .SelectedItems(1)

End If
End With

'MsgBox varDatei
If varDatei = False Then
MsgBox "Sie haben die Auswahl abgebrochen.", vbInformation
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
True, AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, _
AllowDeletingRows:=True
Application.ScreenUpdating = True
ActiveWindow.SelectedSheets.Visible = False
Sheets("Projektdaten").Select
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
True, AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, _
AllowDeletingRows:=True
Application.ScreenUpdating = True
Exit Sub
Else
'MsgBox "Folgende Datei wurde ausgewählt:" & vbCrLf & varDatei
End If

'TXT-Datei einfügen
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & varDatei, _
Destination:=Range("$A$1"))
.name = "Projektdaten.txt"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 2, 9, 2, 2, 2, 9, 2, 2, 2, 2, 2, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

ChrisFragtGern 
Beitragsersteller
 22.08.2017, 14:54

Oui dankeschön, das probier ich die Tage mal aus :)

1

Habs jetzt so gemacht, danke für die Hilfe ;)

Dim intChoice As Integer
Dim strPath As String

'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)

End If

Set workbooks2 = Workbooks.Open(strPath)