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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| Option Explicit
'Widestring open,save filename
'Ex:
'Dim S As String
'Dim Filter As String
' Filter = "Toutes *.bmp *.jpg *.gif|*.bmp;*.jpg;*.jpeg;*.gif"
' Filter = Filter & "|images dib *.bmp|*.bmp"
' Filter = Filter & "|image jpg|*.jpg;*.jpeg"
' Filter = Filter & "|image gif|*.gif"
' If PromptForFileName(S, Filter) Then
' MsgBox S
' End If
Private Declare Function GetOpenFileNameW Lib "comdlg32" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileNameW Lib "comdlg32" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hWndOwner As Long
hInstance As Long
lpstrFilter As Long
lpstrCustomFilter As Long
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As Long
nMaxFile As Long
lpstrFileTitle As Long
nMaxFileTitle As Long
lpstrInitialDir As Long
lpstrTitle As Long
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As Long
End Type
Private Const MAX_PATH = 260
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_FILEMUSTEXIST = &H1000
Public Function PromptForFileName(Var_FileName As String, _
Optional AFilter As String = "", _
Optional ADefaultExt As String = "", _
Optional ATitle As String = "", _
Optional AInitialDir As String = "", _
Optional SaveDialog As Boolean = False) As Boolean
Dim Dialog As OPENFILENAME
Dim szFilter As String
Dim strFile As String
Dim I As Long
strFile = Space(MAX_PATH)
For I = 1 To Len(AFilter)
If Mid$(AFilter, I, 1) = "|" Then
szFilter = szFilter & Chr$(0)
Else
szFilter = szFilter & Mid$(AFilter, I, 1)
End If
Next
szFilter = szFilter & Chr$(0)
With Dialog
.lStructSize = LenB(Dialog)
.hWndOwner = MainHwnd
.lpstrFilter = StrPtr(szFilter)
.lpstrFile = StrPtr(strFile)
.nMaxFile = MAX_PATH
.lpstrTitle = StrPtr(ATitle)
.lpstrInitialDir = StrPtr(AInitialDir)
.lpstrDefExt = StrPtr(ADefaultExt)
.flags = OFN_OVERWRITEPROMPT Or OFN_HIDEREADONLY Or OFN_FILEMUSTEXIST
End With
If SaveDialog Then
PromptForFileName = GetSaveFileNameW(Dialog)
Else
PromptForFileName = GetOpenFileNameW(Dialog)
End If
If PromptForFileName Then
Var_FileName = Trim$(strFile)
End If
End Function |
Partager