This is the API Code for the OpenFileDialog
First the Declarations, constants and types
Code:
Public Const OFN_SHOWHELP = &H10
Public Const OFN_SHARENOWARN = 1
Public Const OFN_SHAREFALLTHROUGH = 2
Public Const OFN_SHAREAWARE = &H4000
Public Const OFN_READONLY = &H1
Public Const OFN_PATHMUSTEXIST = &H800
Public Const OFN_OVERWRITEPROMPT = &H2
Public Const OFN_NOVALIDATE = &H100
Public Const OFN_NOTESTFILECREATE = &H10000
Public Const OFN_NOREADONLYRETURN = &H8000
Public Const OFN_NONETWORKBUTTON = &H20000
Public Const OFN_NOLONGNAMES = &H40000
Public Const OFN_NODEREFERENCELINKS = &H100000
Public Const OFN_NOCHANGEDIR = &H8
Public Const OFN_LONGNAMES = &H200000
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_EXTENSIONDIFFERENT = &H400
Public Const OFN_EXPLORER = &H80000
Public Const OFN_ENABLETEMPLATEHANDLE = &H80
Public Const OFN_ENABLETEMPLATE = &H40
Public Const OFN_ENABLEHOOK = &H20
Public Const OFN_CREATEPROMPT = &H2000
Public Const OFN_ALLOWMULTISELECT = &H200
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename 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 String
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
And then an example code
Code:
' Call the Open File dialog box and look for *.txt files
Dim filebox As OPENFILENAME ' structure that sets the dialog box
Dim fname As String ' will receive selected file's name
Dim retval As Long ' return value
' Configure how the dialog box will look
filebox.hwndOwner = Me.hWnd ' handle of the form calling the function
filebox.lpstrTitle = "Bestand openen" ' text displayed in the box's title bar
' The next line sets up the file types drop-box
filebox.lpstrFilter = "All Files" & vbNullChar & "*.*" & vbNullChar & vbNullChar
filebox.lpstrFile = Space(254) ' initalize buffer that receives path and filename of file
filebox.nMaxFile = 254 ' length of file and pathname buffer
filebox.lpstrFileTitle = Space(254) ' initialize buffer that receives filename of file
filebox.nMaxFileTitle = 254 ' length of filename buffer
' Allow only existing files and hide the read-only check box
filebox.FLAGS = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
filebox.lpstrInitialDir = "C:\"
filebox.hInstance = 0
filebox.lStructSize = Len(filebox) ' the size of the structure
' Execute the dialog box
retval = GetOpenFileName(filebox)
If retval <> 0 Then ' if the dialog box completed successfully
' Remove null space from the file name
fname = Left(filebox.lpstrFile, InStr(filebox.lpstrFile, vbNullChar) - 1)
text1.Text = fname
End If
This is the code that I use. It works in VBA and VB6.
ofcourse Text1 is a textbox where the selected filename is placed.
[email protected]
[email protected]