
API Conversion 16 bit to 32 bit
So many people, including myself a couple of weeks ago, ask for this! I
don't know why MS doesn't post the solution. Paste this code into a
module. All the parameters are optional. There is a parameter for
"filterindex" (=1, 2 or whatever, depending on whether you want the first,
second, ect ext. to be your filter.), but the DefaultExt is easier to use.
The function returns the full file and path. Good luck!
Option Compare Database
Option Explicit
'
'Function adapted from Microsoft Access 95 How-To by Getz and Litwin
'Chapter 4.8
'
Type tagOPENFILENAME
lStructSize As Long
hWndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
NFilterIndex 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
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
' You won't use these.
'Global Const ahtOFN_ENABLEHOOK = &H20
'Global Const ahtOFN_ENABLETEMPLATE = &H40
'Global Const ahtOFN_ENABLETEMPLATEHANDLE = &H80
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
' New for Windows 95
Global Const ahtOFN_EXPLORER = &H80000
Global Const ahtOFN_NODEREFERENCELINKS = &H100000
Global Const ahtOFN_LONGNAMES = &H200000
Function OpenCommDlg(Optional ByVal InitialDir 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
'USAGE:
'OpenCommDlg(InitialDir:="C:\",
DefaultExt:="*.DOC",DialogTitle:="Select your document!")
'
' This is the entry point you'll use to call the common
' file open/save dialog. The parameters are listed
' below, and all are optional.
'
' In:
' InitialDir: the directory in which to first look
'
' 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.
' You can user either FilterIndex or DefaultExt. The DefaultExt
sets the FilterIndex in the function.
' FileName: Default value for the file name text box.
' DialogTitle: Title for the dialog.
' hWnd: parent window handle
' OpenFile: Boolean(True=Open File/False=Save As)
' Out:
' Return Value: Either Null or the selected filename
Dim ofn As tagOPENFILENAME
Dim strFileName As String
Dim strFileTitle, Message As String
Dim fResult As Boolean
Dim Flags As Variant
'-------------------------------------------------------------------
Dim lngResult As Long
'-------------------------------------------------------------------
'Set flags for various features. To set a flag, set its IF statement
to true.
'Flags: one or more of the ahtOFN_* constants, OR'd together.
'
Dim lngFlags As Long
lngFlags = 0
'Prompt before overwriting?
If False Then lngFlags = lngFlags Or ahtOFN_OVERWRITEPROMPT
'Hide the read-only check box?
If True Then lngFlags = lngFlags Or ahtOFN_HIDEREADONLY
'Show the Help button?
If False Then lngFlags = lngFlags Or ahtOFN_SHOWHELP
'Skip filename validation?
If False Then lngFlags = lngFlags Or ahtOFN_NOVALIDATE
'Allow multiple selections?
If False Then lngFlags = lngFlags Or ahtOFN_ALLOWMULTISELECT
'Prompt if specified path doesn't exist?
If False Then lngFlags = lngFlags Or ahtOFN_PATHMUSTEXIST
'Prompt if specified file doesn't exist for opening?
If False Then lngFlags = lngFlags Or ahtOFN_FILEMUSTEXIST
'Prompt if necessary to create new file?
If False Then lngFlags = lngFlags Or ahtOFN_CREATEPROMPT
'Use Explorer look (when options would override this?)
If False Then lngFlags = lngFlags Or ahtOFN_EXPLORER
'Show link names, rather than linked reference for links?
If False Then lngFlags = lngFlags Or ahtOFN_NODEREFERENCELINKS
'Hide the network button?
If False Then lngFlags = lngFlags Or ahtOFN_NONETWORKBUTTON
'
'End of flag setting block
'-------------------------------------------------------------------
'
'-------------------------------------------------------------------
'Block to define the "File of Type" filter for showing filenames.
'Add or comment out as you wish. They will be displayed in the Common
Dialog Box in the
'order shown here. The Filter Index variable specifies which filter
will be displayed
'first, as a default.
'
Dim Filter As String
Filter = Filter & "Word Files (*.doc)" & vbNullChar & "*.DOC" &
vbNullChar
Filter = Filter & "Access Files (*.mda, *.mdb)" & vbNullChar &
"*.MDA;*.MDB" & vbNullChar
Filter = Filter & "dBASE Files (*.dbf)" & vbNullChar & "*.DBF" &
vbNullChar
Filter = Filter & "Text Files (*.txt)" & vbNullChar & "*.TXT" &
vbNullChar
Filter = Filter & "EXE Files (*.exe)" & vbNullChar & "*.EXE" &
vbNullChar
Filter = Filter & "All Files (*.*)" & vbNullChar & "*.*" & vbNullChar
'The Filter Index sets which filter will be used as the default. You
do not need it, but
'it's here if you do.
If Not IsMissing(DefaultExt) Then
If DefaultExt = "*.DOC" Then
FilterIndex = 1
ElseIf DefaultExt = "*.MDB" Then
FilterIndex = 2
ElseIf DefaultExt = "*.DBF" Then
FilterIndex = 3
ElseIf DefaultExt = "*.TXT" Then
FilterIndex = 4
ElseIf DefaultExt = "*.EXE" Then
FilterIndex = 5
Else
FilterIndex = 6
End If
End If
'
'End of block to define filter.
'-------------------------------------------------------------------
'
'-------------------------------------------------------------------
'Initialize any missing parameters that were not passed by the call, or
initialized in this function.
'
If IsMissing(InitialDir) Then InitialDir = CurDir
If IsMissing(Filter) Then Filter = ""
If IsMissing(FilterIndex) Then FilterIndex = 1
If IsMissing(lngFlags) Then Flags = 0&
If IsMissing(DefaultExt) Then DefaultExt = ""
If IsMissing(filename) Then filename = ""
If IsMissing(DialogTitle) Then DialogTitle = ""
If IsMissing(Hwnd) Then Hwnd = Application.hWndAccessApp
If IsMissing(OpenFile) Then OpenFile = True
'
'End if parameter initialization block
'-------------------------------------------------------------------
'
' 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)
.hWndOwner = Hwnd
.strFilter = Filter
.NFilterIndex = FilterIndex
.strFile = strFileName
.nMaxFile = Len(strFileName)
.strFileTitle = strFileTitle
.nMaxFileTitle = Len(strFileTitle)
.strTitle = DialogTitle
.Flags = Flags
.strDefExt = DefaultExt
.strInitialDir = InitialDir
' Didn't think most people would want to deal with
' these options.
.hInstance = 0
.strCustomFilter = ""
.nMaxCustFilter = 0
.lpfnHook = 0
End With
' This will pass the desired data structure to the
' Windows API, which will in turn it uses to display
' the Open/Save As Dialog.
If OpenFile Then
fResult = aht_apiGetOpenFileName(ofn)
Else
fResult = aht_apiGetSaveFileName(ofn)
End If
'
' The function call filled in the strFileTitle member
' of the structure. You'll have to write special code
' to retrieve that if you're interested.
'
If fResult Then
' You might care to check the Flags member of the
' structure to get information about the chosen file.
' In this example, if you bothered to pass in a
' value for Flags, we'll fill it in with the outgoing
' Flags value.
If Not IsMissing(Flags) Then Flags = ofn.Flags
'Trim the NULLS off the file mame.
Dim intPos As Integer
intPos = InStr(ofn.strFile, vbNullChar)
If intPos > 0 Then
OpenCommDlg = Left(ofn.strFile, intPos - 1)
Else
OpenCommDlg = ofn.strFile
End If
Else
OpenCommDlg = Null 'NULL caused problems when the calling function
declared a String type.
'OpenCommDlg = ""
End If
If Len(OpenCommDlg) > 0 Or Not IsNull(OpenCommDlg) Then
'Message$ = "The file you chose was " + ofn.strFile
'MsgBox Message$
Else
Message$ = "No file was selected"
MsgBox Message$
End If
End Function
--
Mike Freeland
Remove the "ROUNDFILE" in the return address.
Sorry for the inconvenience.
Martin Streeter
...
read more »