Ik heb een vba-macro die een bestand zonder extensie moet openen als csv. De naam van het bestand wordt opgehaald via een functie:
Dit levert de juiste bestandsnaam op, zonder extensie.
Als ik deze vervolgens inlaadt via:
Krijg ik de foutmelding dat bestand.xlsx niet gevonden is. Er wordt dus een .xlsx toegevoegd. Hoe kan ik dit bestand nu toch openen. Als ik handmatig het bestand selecteer is het geen probleem.
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| Function NewestFile(Directory, FileSpec) ' Returns the name of the most recent file in a Directory ' That matches the FileSpec (e.g., "*.xls"). ' Returns an empty string if the directory does not exist or ' it contains no matching files Dim FileName As String Dim MostRecentFile As String Dim MostRecentDate As Date If Right(Directory, 1) <> "\" Then Directory = Directory & "\" FileName = Dir(Directory & FileSpec, 0) If FileName <> "" Then MostRecentFile = FileName MostRecentDate = FileDateTime(Directory & FileName) Do While FileName <> "" If FileDateTime(Directory & FileName) > MostRecentDate Then MostRecentFile = FileName MostRecentDate = FileDateTime(Directory & FileName) End If FileName = Dir Loop End If NewestFile = MostRecentFile End Function |
Visual Basic:
1
2
| strPath = NewestFile("Directory", "EXT*") MsgBox strPath |
Dit levert de juiste bestandsnaam op, zonder extensie.
Als ik deze vervolgens inlaadt via:
Visual Basic:
1
| Workbooks.OpenText FileName:=strPath, Semicolon:=True |
Krijg ik de foutmelding dat bestand.xlsx niet gevonden is. Er wordt dus een .xlsx toegevoegd. Hoe kan ik dit bestand nu toch openen. Als ik handmatig het bestand selecteer is het geen probleem.