Io facevo così:
al click del pulsante "apri":
Dim fb As OPENFILENAME
Call ShowFile(Me, fb)
fName = Left(fb.lpstrFile, InStr(fb.lpstrFile, vbNullChar) - 1) 'percorso completo
Su un Modulo di Classe:
Option Explicit
Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_PATHMUSTEXIST = &H800
Public Declare Function GetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (lpofn As OPENFILENAME) As Long
Public Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustomFilter 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
Public Sub ShowFile(frm As Form, fb As OPENFILENAME)
Dim fName As String
Dim result As Long
Dim path As String
path = "C:\"
With fb
.lStructSize = Len(fb) 'Size of the structure
.hwndOwner = frm.Hwnd 'Handle to window opening the dialog
.hInstance = 0 'Handle to calling instance (not needed)
.lpstrFilter = "Tutti i file" & vbNullChar & "*.*" & vbNullChar
.nMaxCustomFilter = 0
.nFilterIndex = 1 'Default filter is the first one
.lpstrFile = Space(256) & vbNullChar 'No default filename. Also make room for received path and _
filename of the user's selection.
.nMaxFile = Len(.lpstrFile)
.lpstrFileTitle = Space(256) & vbNullChar 'Make room for filename of the user's selection
.nMaxFileTitle = Len(.lpstrFileTitle)
.lpstrInitialDir = path & vbNullChar 'Initial directory is C:\
.lpstrTitle = "Apri file" & vbNullChar 'Title of file dialog
.flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
.nFileOffset = 0 'The rest of the options aren't needed.
.nFileExtension = 0
.lCustData = 0
.lpfnHook = 0
End With
result = GetOpenFileName(fb)
If result <> 0 Then
fName = Left(fb.lpstrFile, InStr(fb.lpstrFile, vbNullChar) - 1)
End If
End Sub
Ciao![]()