Bonjour,
Voici une fonction "standard" qui se positionne sur le répertoire choisi et retourne un nom de fichier (à mettre dans un module) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| Public Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
'Pour chercher un fichier dans l'explorateur (fonction publique)
Public Function Parcourt(fenetre, repertoire As String, Optional extension As String = "", Optional titre As String = "Choix du fichier") As String
If Not Mode_debug Then On Error GoTo err:
Dim OFName As OPENFILENAME
Dim a As Integer, res As String
100 res = ""
102 OFName.lStructSize = Len(OFName) 'Set the structure size
104 OFName.hwndOwner = fenetre 'Set the owner window
106 OFName.hInstance = Application.hWndAccessApp 'Set the application's instance
'Set the filet
108 OFName.lpstrFilter = extension & "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
110 OFName.lpstrFile = Space$(254) 'Create a buffer
112 OFName.nMaxFile = 255 'Set the maximum number of chars
114 OFName.lpstrFileTitle = Space$(254) 'Create a buffer
116 OFName.nMaxFileTitle = 255 'Set the maximum number of chars
118 OFName.lpstrInitialDir = repertoire 'Set the initial directory
120 OFName.lpstrTitle = titre
122 OFName.flags = 0 'no extra flags
'Show the 'Open File'-dialog
124 If GetOpenFileName(OFName) Then
'on recupere 254 caractere il faut limiter au fichier et trouver chr(0)
126 a = InStr(OFName.lpstrFile, Chr(0))
128 res = Left(OFName.lpstrFile, a - 1)
End If
130 Parcourt = res
Exit Function
err: Call message("Erreur " & err.Number & "/" & Erl & " dans fonctions.parcourt : " & err.description)
End Function |
Exemple d'appel avec filtre sur excel
fichier = Parcourt(Me.hwnd, "c:\users\documents", "excel (*.xls*)" & Chr$(0) & "*.xls*" & Chr$(0))
Partager