Retrieving a File Path

The following API enables you to create a custom file browser. The program example using the API customizes the function call to create a browser for a specific need—in this case, returning the file path of a user-selected file:

Type tagOPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long strFilter As String strCustomFilter As String nMaxCustFilter As Long nFilterlndex As Long strFile As String nMaxFile As Long strFileTitle As String nMaxFileTitle As Long strInitialDir As String strTitle As String Flags As Long nFileOffset As Integer nFileExtension As Integer strDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type

Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _

Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _

Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long

Global Const ahtOFN_READONLY = &H1 Global Const ahtOFN_OVERWRITEPROMPT = &H2 Global Const ahtOFN HIDEREADONLY = &H4

Global Const ahtOFN_NOCHANGEDIR = &H8 Global Const ahtOFN_SHOWHELP = &H10 Global Const ahtOFN_NOVALIDATE = &H100 Global Const ahtOFN_ALLOWMULTISELECT = &H200 Global Const ahtOFN_EXTENSIONDIFFERENT = &H400 Global Const ahtOFN_PATHMUSTEXIST = &H800 Global Const ahtOFN_FILEMUSTEXIST = &H1000 Global Const ahtOFN_CREATEPROMPT = &H2000 Global Const ahtOFN_SHAREAWARE = &H4000 Global Const ahtOFN_NOREADONLYRETURN = &H8000 Global Const ahtOFN_NOTESTFILECREATE = &H10000 Global Const ahtOFN_NONETWORKBUTTON = &H20000 Global Const ahtOFN_NOLONGNAMES = &H40000 Global Const ahtOFN_EXPLORER = &H80000 Global Const ahtOFN_NODEREFERENCELINKS = &H100000 Global Const ahtOFN LONGNAMES = &H200000

Function ahtCommonFileOpenSave( _

Optional ByRef Flags As Variant, _ Optional ByVal InitialDir As Variant, _ Optional ByVal Filter As Variant, _ Optional ByVal Filterindex As Variant, _ Optional ByVal DefaultExt As Variant, _ Optional ByVal FileName As Variant, _ Optional ByVal DialogTitle As Variant, _ Optional ByVal hwnd As Variant, _ Optional ByVal OpenFile As Variant) As Variant

1 This is the entry point you'll use to call the common ' file Open/Save As dialog. The parameters are listed ' below, and all are optional.

' Flags: one or more of the ahtOFN_* constants, OR'd together.

' InitialDir: the directory in which to first look

' Filter: a set of file filters ' (Use AddFilterltem to set up Filters)

' Filterindex: 1-based integer indicating which filter

' set to use, by default (1 if unspecified)

' DefaultExt: Extension to use if the user doesn't enter one.

' Only useful on file saves.

' FileName: Default value for the filename text box.

' DialogTitle: Title for the dialog.

' hWnd: parent window handle

' OpenFile: Boolean(True=Open File/False=Save As)

' Return Value: Either Null or the selected filename

Dim OFN As tagOPENFILENAME Dim strFileName As String Dim strFileTitle As String Dim fResult As Boolean

' Give the dialog a caption title. If IsMissing(InitialDir) Then InitialDir = CurDir If IsMissing(Filter) Then Filter = ""

If IsMissing(FilterIndex) Then Filterlndex = 1 If IsMissing(Flags) Then Flags = 0& If IsMissing(DefaultExt) Then DefaultExt = "" If IsMissing(FileName) Then FileName = "" If IsMissing(DialogTitle) Then DialogTitle = "" If IsMissing(OpenFile) Then OpenFile = True

1 Allocate string space for the returned strings. strFileName = Left(FileName & String(256, 0), 256) strFileTitle = String(256, 0)

' Set up the data structure before you call the function With OFN

.lStructSize = Len(OFN) .strFilter = Filter .nFilterIndex = FilterIndex .strFile = strFileName .nMaxFile = Len(strFileName) .strFileTitle = strFileTitle .nMaxFileTitle = Len(strFileTitle) .strTitle = DialogTitle .Flags = Flags .strDefExt = DefaultExt .strInitialDir = InitialDir .hInstance = 0 .lpfnHook = 0

.strCustomFilter = String(255, 0) .nMaxCustFilter = 255 End With

' This passes the desired data structure to the 1 Windows API, which will in turn displays 1 the Open/Save As dialog. If OpenFile Then fResult = aht_apiGetOpenFileName(OFN)

Else fResult = aht_apiGetSaveFileName(OFN) End If

1 The function call filled in the strFileTitle member ' of the structure. You have to write special code 1 to retrieve that if you're interested. If fResult Then

' You might care to check the Flags member of the 1 structure to get information about the chosen file. ' In this example, if you bothered to pass a 1 value for Flags, we'll fill it in with the outgoing ' Flags value.

If Not IsMissing(Flags) Then Flags = OFN.Flags ahtCommonFileOpenSave = TrimNull(OFN.strFile)

Else ahtCommonFileOpenSave = vbNullString End If

End Function

Function ahtAddFilterItem(strFilter As String, _

strDescription As String, Optional varltem As Variant) As String ' Tack a new chunk onto the file filter.

1 That is, take the old value, stick onto it the description, ' (like "Databases"), a null character, the skeleton 1 (like "*.mdb;*.mda"), and a final null character.

If IsMissing(varltem) Then varltem = "*.*" ahtAddFilterltem = strFilter & strDescription & _ vbNullChar & varltem & vbNullChar

0 0

Post a comment