JamesBing
asked on
GetOpenFileName Problem
Hi experts,
I am new at this and would really appreciate if you can help me with the problem below.
here is the code...
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameW" (pOpenFileName As OPENFILENAME) As Long
Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter 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
Dim openFile As OPENFILENAME
Function GetOpenFile()
Dim APIResults As Long
Dim sFilter As String
'I don't know what goes next here, but I want it to return the filepath AND the filename. I don't want to filter out any files (I want to view all of the files). Hope I don't confused you.
End Function
Thanks guys.
Bing
I am new at this and would really appreciate if you can help me with the problem below.
here is the code...
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameW" (pOpenFileName As OPENFILENAME) As Long
Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter 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
Dim openFile As OPENFILENAME
Function GetOpenFile()
Dim APIResults As Long
Dim sFilter As String
'I don't know what goes next here, but I want it to return the filepath AND the filename. I don't want to filter out any files (I want to view all of the files). Hope I don't confused you.
End Function
Thanks guys.
Bing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Me.hWnd is a handle to the current forms window. Try creating a new project and pasting that code in to see how it works.
Idle_Mind
Idle_Mind
or try replace Me.hWnd with 0& , if there is not handle ?
I was using Access 97 and didn't need to assign any value to "OFName.hwndOwner". I think, "OFName.hInstance" is optional, too.
cheers, Pentabyte
cheers, Pentabyte
Why don't you use CommonDialog ?
Take a look here http://www.freevbcode.com/ShowCode.Asp?ID=631
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In response to Don-Acme's code,
I get the OpenDialog but the selected file doesn't open...
I copied-pasted the code in a new module and "call" it from a click event in Excel 97.
I get the OpenDialog but the selected file doesn't open...
I copied-pasted the code in a new module and "call" it from a click event in Excel 97.
Hi 0MoJoH,
I checked my code and actually it should be working fine on any type of Windows.
I extended the code alot and I've created a very simple class for the use within our company so that nobody has to rewrite it... I'm still looking for the documentation since I've saved it somewhere and I cannot find it...
Hope you can use this code here:
PS: the lines that are set to Remarks are really not necessary for a simple Excel application.
PSPS: this code should be more a guideline to write your own it is definetly not the best you can get
Usage:
--------
1. Create a new class in excel, VB or whatever
2. paste the source code into the class
3. You can leave the default settings for the properties
4. try if you can open any file you need to.
Source Code:
-------------------------- ---------- ---------- ---------- ---------- --------
Option Explicit
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (lpofn As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias _
"GetSaveFileNameA" (lpofn As OPENFILENAME) As Long
Private 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
Enum OFN_FLAGS
OFN_ENABLE_CREATE_PROMPT = &H2000
OFN_ENABLE_MULTISELECT = &H200
OFN_ENABLE_HOOK = &H20
OFN_ENABLE_SIZING = &H800000
OFN_ENABLE_TEMPLATES = &H40
OFN_ENABLE_TEMPLATEHANDLE = &H80
OFN_ENABLE_EXPLORER_STYLE = &H80000
OFN_ENABLE_DIFFERENT_EXTEN SION = &H400
OFN_ENABLE_REFERENCED_FILE LINKS = &H100000
OFN_ENABLE_LONG_FILENAMES = &H200000
OFN_ENABLE_OPEN_AS_READONL Y = &H1
OFN_ENABLE_PATH_MUST_EXIST = &H800
OFN_ENABLE_FILE_MUST_EXIST = &H1000
OFN_ENABLE_HIDE_READONLY_F ILES = &H4
OFN_ENABLE_OVERWRITE_PROMP T = &H2
OFN_ENABLE_HELP_BUTTON = &H10
OFN_ENABLE_IGNORE_SHARED_F ILE_AWAREN ESS = &H4000
OFN_DISABLE_DIRECTORY_CHAN GE = &H8
OFN_DISABLE_LONG_FILENAMES = &H40000
OFN_DISABLE_NETWORK_BUTTON = &H20000
OFN_DISABLE_READONLY_RETUR N = &H8000
OFN_DISABLE_CREATION_OF_TE STFILE = &H10000
OFN_DISABLE_FILENAME_VALID ATION = &H100
End Enum
Dim m_strDefaultInitialDrive As String
Dim m_strDefaultOpenFileTitle As String
Dim m_strDefaultSaveFileTitle As String
Dim m_strDefaultFileType As String
Dim m_hexOpenFileFlags
Dim m_hexSaveFileFlags
Dim m_lngDefaultWindowHandle As Long
Dim m_lngDefaultExtensionIndex As Long
Dim m_lngDefaultEmptyStringFor File As Long
Public Property Let DefaultInitialDrive(ByVal strDefaultInitialDrive As String)
Dim blnIsNotDrive As Boolean
blnIsNotDrive = InStr(strDefaultInitialDri ve, ":\") = 0
If blnIsNotDrive Then
strDefaultInitialDrive = "C:\" & vbNullChar
End If
m_strDefaultInitialDrive = strDefaultInitialDrive & vbNullChar
End Property
Public Property Get DefaultInitialDrive() As String
DefaultInitialDrive = Replace(m_strDefaultInitia lDrive, vbNullChar, "")
End Property
Public Property Let DefaultOpenFileTitle(ByVal strDefaultOpenFileTitle As String)
m_strDefaultOpenFileTitle = strDefaultOpenFileTitle & vbNullChar
End Property
Public Property Get DefaultOpenFileTitle() As String
DefaultOpenFileTitle = Replace(m_strDefaultOpenFi leTitle, vbNullChar, "")
End Property
Public Property Let DefaultSaveFileTitle(ByVal strDefaultSaveFileTitle As String)
m_strDefaultSaveFileTitle = strDefaultSaveFileTitle & vbNullChar
End Property
Public Property Get DefaultSaveFileTitle() As String
DefaultSaveFileTitle = Replace(m_strDefaultSaveFi leTitle, vbNullChar, "")
End Property
Public Property Let DefaultFileType(ByVal strDefaultFileType As String)
Dim strFileType() As String
Dim strDescription As String
Dim strExtension As String
Dim i As Integer
Dim j As Integer
i = 0
j = 1
m_strDefaultFileType = ""
strFileType = Split(strDefaultFileType, ";")
Do Until j > UBound(strFileType)
strDescription = strFileType(i)
strExtension = strFileType(j)
strExtension = Replace(strExtension, ".", "")
strExtension = Replace(strExtension, "*", "")
If strExtension = "" Then
strExtension = "*.*"
Else
strExtension = "*." & strExtension
End If
m_strDefaultFileType = m_strDefaultFileType & strDescription & vbNullChar & strExtension
If j <> UBound(strFileType) Then
m_strDefaultFileType = m_strDefaultFileType & vbNullChar
ElseIf j = UBound(strFileType) Then
m_strDefaultFileType = m_strDefaultFileType & vbNullChar & vbNullChar
End If
i = i + 2
j = j + 2
Loop
End Property
Public Property Get DefaultFileType() As String
DefaultFileType = Replace(m_strDefaultFileTy pe, vbNullChar, ";")
End Property
Public Property Let OpenFileFlags(ByVal hexOpenFileFlags As OFN_FLAGS)
m_hexOpenFileFlags = hexOpenFileFlags
End Property
Public Property Get OpenFileFlags() As OFN_FLAGS
OpenFileFlags = m_hexOpenFileFlags
End Property
Public Property Let SaveFileFlags(ByVal hexSaveFileFlags As OFN_FLAGS)
m_hexSaveFileFlags = hexSaveFileFlags
End Property
Public Property Get SaveFileFlags() As OFN_FLAGS
SaveFileFlags = m_hexSaveFileFlags
End Property
Public Property Let DefaultWindowHandle(ByVal lngDefaultWindowHandle As Long)
m_lngDefaultWindowHandle = lngDefaultWindowHandle
End Property
Public Property Get DefaultWindowHandle() As Long
DefaultWindowHandle = m_lngDefaultWindowHandle
End Property
Public Property Let DefaultExtensionIndex(ByVa l lngDefaultExtensionIndex As Long)
Dim blnIndexIsLessThanOne As Boolean
If blnIndexIsLessThanOne = lngDefaultExtensionIndex < 1 Then
lngDefaultExtensionIndex = 1
End If
m_lngDefaultExtensionIndex = lngDefaultExtensionIndex
End Property
Public Property Get DefaultExtensionIndex() As Long
DefaultExtensionIndex = m_lngDefaultExtensionIndex
End Property
Public Property Let DefaultEmptyStringForFile( ByVal lngDefaultEmptyStringForFi le As Long)
Dim blnIndexIsLess As Boolean
If blnIndexIsLess = lngDefaultEmptyStringForFi le < 256 Then
lngDefaultEmptyStringForFi le = 1024
End If
m_lngDefaultEmptyStringFor File = lngDefaultEmptyStringForFi le
End Property
Public Property Get DefaultEmptyStringForFile( ) As Long
DefaultEmptyStringForFile = m_lngDefaultEmptyStringFor File
End Property
Private Sub Class_Initialize()
m_strDefaultInitialDrive = "C:\" & vbNullChar
m_strDefaultOpenFileTitle = "Open" & vbNullChar
m_strDefaultSaveFileTitle = "Save As..." & vbNullChar
m_strDefaultFileType = "All Files" & vbNullChar & "*.*" & vbNullChar & vbNullChar
m_hexOpenFileFlags = OFN_ENABLE_SIZING Or OFN_ENABLE_EXPLORER_STYLE Or OFN_ENABLE_LONG_FILENAMES Or _
OFN_ENABLE_PATH_MUST_EXIST Or OFN_ENABLE_FILE_MUST_EXIST
m_hexSaveFileFlags = OFN_ENABLE_SIZING Or OFN_ENABLE_EXPLORER_STYLE Or OFN_ENABLE_LONG_FILENAMES Or _
OFN_ENABLE_PATH_MUST_EXIST Or OFN_ENABLE_FILE_MUST_EXIST Or _
OFN_ENABLE_OVERWRITE_PROMP T
m_lngDefaultWindowHandle = 0
m_lngDefaultExtensionIndex = 1
m_lngDefaultEmptyStringFor File = 1024
End Sub
Public Function OpenFileDialog() As String
Dim lngRetval As Long
Dim i As Integer
Dim strFileNamesTmp As String
Dim strFileNames() As String
Dim strSemicolon As String
Dim OFN As OPENFILENAME
With OFN
.lStructSize = Len(OFN)
'.hInstance = App.hInstance
.hwndOwner = m_lngDefaultWindowHandle
.flags = m_hexOpenFileFlags
.lpstrTitle = m_strDefaultOpenFileTitle
.lpstrFilter = m_strDefaultFileType
.nFilterIndex = m_lngDefaultExtensionIndex
.lpstrFile = Space(m_lngDefaultEmptyStr ingForFile ) & vbNullChar
.nMaxFile = Len(.lpstrFile)
.lpstrFileTitle = Space(m_lngDefaultEmptyStr ingForFile ) & vbNullChar
.nMaxFileTitle = Len(.lpstrFileTitle)
.lpstrInitialDir = m_strDefaultInitialDrive
'.lpfnHook = GetAddress(AddressOf HookFunc)
End With
lngRetval = GetOpenFileName(OFN)
If lngRetval = 0 Then
OpenFileDialog = ""
Else
strFileNames = Split(Trim(OFN.lpstrFile), vbNullChar & vbNullChar)
If UBound(strFileNames) > 0 Then
strFileNamesTmp = Left$(OFN.lpstrFile, InStr(1, OFN.lpstrFile, vbNullChar & vbNullChar) - 1)
strFileNames = Split(Trim(strFileNamesTmp ), vbNullChar)
If UBound(strFileNames) = 0 Then
GoTo EndSub
End If
i = 1
strFileNamesTmp = ""
Do Until i > UBound(strFileNames)
Select Case UBound(strFileNames)
Case Is = i
strSemicolon = ""
Case Else
strSemicolon = ";"
End Select
strFileNamesTmp = strFileNamesTmp & Trim(strFileNames(0)) & _
Trim(strFileNames(i)) & _
strSemicolon
i = i + 1
Loop
Else
strFileNamesTmp = Left$(OFN.lpstrFile, InStr(1, OFN.lpstrFile, vbNullChar) - 1)
End If
EndSub:
OpenFileDialog = Trim(strFileNamesTmp)
End If
End Function
Public Function SaveFileDialog() As String
Dim lngRetval As Long
Dim strFileNamesTmp As String
Dim OFN As OPENFILENAME
With OFN
.lStructSize = Len(OFN)
'.hInstance = App.hInstance
.hwndOwner = m_lngDefaultWindowHandle
.flags = m_hexSaveFileFlags
.lpstrTitle = m_strDefaultSaveFileTitle
.lpstrFilter = m_strDefaultFileType
.nFilterIndex = m_lngDefaultExtensionIndex
.lpstrFile = Space(m_lngDefaultEmptyStr ingForFile ) & vbNullChar
.nMaxFile = m_lngDefaultEmptyStringFor File + 1
.lpstrFileTitle = Space(m_lngDefaultEmptyStr ingForFile ) & vbNullChar
.nMaxFileTitle = m_lngDefaultEmptyStringFor File + 1
.lpstrInitialDir = m_strDefaultInitialDrive
'.lpfnHook = GetAddress(Address Of Hook Func)
End With
lngRetval = GetSaveFileName(OFN)
If lngRetval = 0 Then
SaveFileDialog = ""
Else
strFileNamesTmp = Left$(OFN.lpstrFile, InStr(1, OFN.lpstrFile, vbNullChar) - 1)
SaveFileDialog = Trim(strFileNamesTmp)
End If
End Function
-------------------------- ---------- ---------- ---------- ---------- --------
I checked my code and actually it should be working fine on any type of Windows.
I extended the code alot and I've created a very simple class for the use within our company so that nobody has to rewrite it... I'm still looking for the documentation since I've saved it somewhere and I cannot find it...
Hope you can use this code here:
PS: the lines that are set to Remarks are really not necessary for a simple Excel application.
PSPS: this code should be more a guideline to write your own it is definetly not the best you can get
Usage:
--------
1. Create a new class in excel, VB or whatever
2. paste the source code into the class
3. You can leave the default settings for the properties
4. try if you can open any file you need to.
Source Code:
--------------------------
Option Explicit
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (lpofn As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias _
"GetSaveFileNameA" (lpofn As OPENFILENAME) As Long
Private 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
Enum OFN_FLAGS
OFN_ENABLE_CREATE_PROMPT = &H2000
OFN_ENABLE_MULTISELECT = &H200
OFN_ENABLE_HOOK = &H20
OFN_ENABLE_SIZING = &H800000
OFN_ENABLE_TEMPLATES = &H40
OFN_ENABLE_TEMPLATEHANDLE = &H80
OFN_ENABLE_EXPLORER_STYLE = &H80000
OFN_ENABLE_DIFFERENT_EXTEN
OFN_ENABLE_REFERENCED_FILE
OFN_ENABLE_LONG_FILENAMES = &H200000
OFN_ENABLE_OPEN_AS_READONL
OFN_ENABLE_PATH_MUST_EXIST
OFN_ENABLE_FILE_MUST_EXIST
OFN_ENABLE_HIDE_READONLY_F
OFN_ENABLE_OVERWRITE_PROMP
OFN_ENABLE_HELP_BUTTON = &H10
OFN_ENABLE_IGNORE_SHARED_F
OFN_DISABLE_DIRECTORY_CHAN
OFN_DISABLE_LONG_FILENAMES
OFN_DISABLE_NETWORK_BUTTON
OFN_DISABLE_READONLY_RETUR
OFN_DISABLE_CREATION_OF_TE
OFN_DISABLE_FILENAME_VALID
End Enum
Dim m_strDefaultInitialDrive As String
Dim m_strDefaultOpenFileTitle As String
Dim m_strDefaultSaveFileTitle As String
Dim m_strDefaultFileType As String
Dim m_hexOpenFileFlags
Dim m_hexSaveFileFlags
Dim m_lngDefaultWindowHandle As Long
Dim m_lngDefaultExtensionIndex
Dim m_lngDefaultEmptyStringFor
Public Property Let DefaultInitialDrive(ByVal strDefaultInitialDrive As String)
Dim blnIsNotDrive As Boolean
blnIsNotDrive = InStr(strDefaultInitialDri
If blnIsNotDrive Then
strDefaultInitialDrive = "C:\" & vbNullChar
End If
m_strDefaultInitialDrive = strDefaultInitialDrive & vbNullChar
End Property
Public Property Get DefaultInitialDrive() As String
DefaultInitialDrive = Replace(m_strDefaultInitia
End Property
Public Property Let DefaultOpenFileTitle(ByVal
m_strDefaultOpenFileTitle = strDefaultOpenFileTitle & vbNullChar
End Property
Public Property Get DefaultOpenFileTitle() As String
DefaultOpenFileTitle = Replace(m_strDefaultOpenFi
End Property
Public Property Let DefaultSaveFileTitle(ByVal
m_strDefaultSaveFileTitle = strDefaultSaveFileTitle & vbNullChar
End Property
Public Property Get DefaultSaveFileTitle() As String
DefaultSaveFileTitle = Replace(m_strDefaultSaveFi
End Property
Public Property Let DefaultFileType(ByVal strDefaultFileType As String)
Dim strFileType() As String
Dim strDescription As String
Dim strExtension As String
Dim i As Integer
Dim j As Integer
i = 0
j = 1
m_strDefaultFileType = ""
strFileType = Split(strDefaultFileType, ";")
Do Until j > UBound(strFileType)
strDescription = strFileType(i)
strExtension = strFileType(j)
strExtension = Replace(strExtension, ".", "")
strExtension = Replace(strExtension, "*", "")
If strExtension = "" Then
strExtension = "*.*"
Else
strExtension = "*." & strExtension
End If
m_strDefaultFileType = m_strDefaultFileType & strDescription & vbNullChar & strExtension
If j <> UBound(strFileType) Then
m_strDefaultFileType = m_strDefaultFileType & vbNullChar
ElseIf j = UBound(strFileType) Then
m_strDefaultFileType = m_strDefaultFileType & vbNullChar & vbNullChar
End If
i = i + 2
j = j + 2
Loop
End Property
Public Property Get DefaultFileType() As String
DefaultFileType = Replace(m_strDefaultFileTy
End Property
Public Property Let OpenFileFlags(ByVal hexOpenFileFlags As OFN_FLAGS)
m_hexOpenFileFlags = hexOpenFileFlags
End Property
Public Property Get OpenFileFlags() As OFN_FLAGS
OpenFileFlags = m_hexOpenFileFlags
End Property
Public Property Let SaveFileFlags(ByVal hexSaveFileFlags As OFN_FLAGS)
m_hexSaveFileFlags = hexSaveFileFlags
End Property
Public Property Get SaveFileFlags() As OFN_FLAGS
SaveFileFlags = m_hexSaveFileFlags
End Property
Public Property Let DefaultWindowHandle(ByVal lngDefaultWindowHandle As Long)
m_lngDefaultWindowHandle = lngDefaultWindowHandle
End Property
Public Property Get DefaultWindowHandle() As Long
DefaultWindowHandle = m_lngDefaultWindowHandle
End Property
Public Property Let DefaultExtensionIndex(ByVa
Dim blnIndexIsLessThanOne As Boolean
If blnIndexIsLessThanOne = lngDefaultExtensionIndex < 1 Then
lngDefaultExtensionIndex = 1
End If
m_lngDefaultExtensionIndex
End Property
Public Property Get DefaultExtensionIndex() As Long
DefaultExtensionIndex = m_lngDefaultExtensionIndex
End Property
Public Property Let DefaultEmptyStringForFile(
Dim blnIndexIsLess As Boolean
If blnIndexIsLess = lngDefaultEmptyStringForFi
lngDefaultEmptyStringForFi
End If
m_lngDefaultEmptyStringFor
End Property
Public Property Get DefaultEmptyStringForFile(
DefaultEmptyStringForFile = m_lngDefaultEmptyStringFor
End Property
Private Sub Class_Initialize()
m_strDefaultInitialDrive = "C:\" & vbNullChar
m_strDefaultOpenFileTitle = "Open" & vbNullChar
m_strDefaultSaveFileTitle = "Save As..." & vbNullChar
m_strDefaultFileType = "All Files" & vbNullChar & "*.*" & vbNullChar & vbNullChar
m_hexOpenFileFlags = OFN_ENABLE_SIZING Or OFN_ENABLE_EXPLORER_STYLE Or OFN_ENABLE_LONG_FILENAMES Or _
OFN_ENABLE_PATH_MUST_EXIST
m_hexSaveFileFlags = OFN_ENABLE_SIZING Or OFN_ENABLE_EXPLORER_STYLE Or OFN_ENABLE_LONG_FILENAMES Or _
OFN_ENABLE_PATH_MUST_EXIST
OFN_ENABLE_OVERWRITE_PROMP
m_lngDefaultWindowHandle = 0
m_lngDefaultExtensionIndex
m_lngDefaultEmptyStringFor
End Sub
Public Function OpenFileDialog() As String
Dim lngRetval As Long
Dim i As Integer
Dim strFileNamesTmp As String
Dim strFileNames() As String
Dim strSemicolon As String
Dim OFN As OPENFILENAME
With OFN
.lStructSize = Len(OFN)
'.hInstance = App.hInstance
.hwndOwner = m_lngDefaultWindowHandle
.flags = m_hexOpenFileFlags
.lpstrTitle = m_strDefaultOpenFileTitle
.lpstrFilter = m_strDefaultFileType
.nFilterIndex = m_lngDefaultExtensionIndex
.lpstrFile = Space(m_lngDefaultEmptyStr
.nMaxFile = Len(.lpstrFile)
.lpstrFileTitle = Space(m_lngDefaultEmptyStr
.nMaxFileTitle = Len(.lpstrFileTitle)
.lpstrInitialDir = m_strDefaultInitialDrive
'.lpfnHook = GetAddress(AddressOf HookFunc)
End With
lngRetval = GetOpenFileName(OFN)
If lngRetval = 0 Then
OpenFileDialog = ""
Else
strFileNames = Split(Trim(OFN.lpstrFile),
If UBound(strFileNames) > 0 Then
strFileNamesTmp = Left$(OFN.lpstrFile, InStr(1, OFN.lpstrFile, vbNullChar & vbNullChar) - 1)
strFileNames = Split(Trim(strFileNamesTmp
If UBound(strFileNames) = 0 Then
GoTo EndSub
End If
i = 1
strFileNamesTmp = ""
Do Until i > UBound(strFileNames)
Select Case UBound(strFileNames)
Case Is = i
strSemicolon = ""
Case Else
strSemicolon = ";"
End Select
strFileNamesTmp = strFileNamesTmp & Trim(strFileNames(0)) & _
Trim(strFileNames(i)) & _
strSemicolon
i = i + 1
Loop
Else
strFileNamesTmp = Left$(OFN.lpstrFile, InStr(1, OFN.lpstrFile, vbNullChar) - 1)
End If
EndSub:
OpenFileDialog = Trim(strFileNamesTmp)
End If
End Function
Public Function SaveFileDialog() As String
Dim lngRetval As Long
Dim strFileNamesTmp As String
Dim OFN As OPENFILENAME
With OFN
.lStructSize = Len(OFN)
'.hInstance = App.hInstance
.hwndOwner = m_lngDefaultWindowHandle
.flags = m_hexSaveFileFlags
.lpstrTitle = m_strDefaultSaveFileTitle
.lpstrFilter = m_strDefaultFileType
.nFilterIndex = m_lngDefaultExtensionIndex
.lpstrFile = Space(m_lngDefaultEmptyStr
.nMaxFile = m_lngDefaultEmptyStringFor
.lpstrFileTitle = Space(m_lngDefaultEmptyStr
.nMaxFileTitle = m_lngDefaultEmptyStringFor
.lpstrInitialDir = m_strDefaultInitialDrive
'.lpfnHook = GetAddress(Address Of Hook Func)
End With
lngRetval = GetSaveFileName(OFN)
If lngRetval = 0 Then
SaveFileDialog = ""
Else
strFileNamesTmp = Left$(OFN.lpstrFile, InStr(1, OFN.lpstrFile, vbNullChar) - 1)
SaveFileDialog = Trim(strFileNamesTmp)
End If
End Function
--------------------------
Hi DOn-Acme,
Thanks for your help.
This updated code is 5 times harder to understand than the initial one that you posted.
I guess I will have a lot of fun decrypting your "simple" class.
Ok.
I created a new class and pasted the code source.
Now I have no idea how to use it.
I mean,... following JamesBing's idea, the objective was to allow the user to browse and open a file via a dialog box.
How do I call the OpenFile function from a button ?
Which variable will be containing the name of the file that has been selected by the user to open ?
My god I wish my company was using Excel 2003 instead of 97.........
Thanks for your help.
This updated code is 5 times harder to understand than the initial one that you posted.
I guess I will have a lot of fun decrypting your "simple" class.
Ok.
I created a new class and pasted the code source.
Now I have no idea how to use it.
I mean,... following JamesBing's idea, the objective was to allow the user to browse and open a file via a dialog box.
How do I call the OpenFile function from a button ?
Which variable will be containing the name of the file that has been selected by the user to open ?
My god I wish my company was using Excel 2003 instead of 97.........
Hey 0MoJoH,
there are some properties, that will let you set specific settings for your Open File Button
This procedure here:
contains the default settings when you call the class.
-------------------------- ---------- ---------- ---
Private Sub Class_Initialize()
m_strDefaultInitialDrive = "C:\" & vbNullChar
m_strDefaultOpenFileTitle = "Open" & vbNullChar
m_strDefaultSaveFileTitle = "Save As..." & vbNullChar
m_strDefaultFileType = "All Files" & vbNullChar & "*.*" & vbNullChar & vbNullChar
m_hexOpenFileFlags = OFN_ENABLE_SIZING Or OFN_ENABLE_EXPLORER_STYLE Or OFN_ENABLE_LONG_FILENAMES Or _
OFN_ENABLE_PATH_MUST_EXIST Or OFN_ENABLE_FILE_MUST_EXIST
m_hexSaveFileFlags = OFN_ENABLE_SIZING Or OFN_ENABLE_EXPLORER_STYLE Or OFN_ENABLE_LONG_FILENAMES Or _
OFN_ENABLE_PATH_MUST_EXIST Or OFN_ENABLE_FILE_MUST_EXIST Or _
OFN_ENABLE_OVERWRITE_PROMP T
m_lngDefaultWindowHandle = 0
m_lngDefaultExtensionIndex = 1
m_lngDefaultEmptyStringFor File = 1024
End Sub
-------------------------- ---------- ---------- ---
The variable names are set to the same names as the properties you have.
-------------------------- ---------- ---------- ---
To open a file use: OpenFileDialog
To save a file use: SaveFileDialog
-------------------------- ---------- ---------- ---
-------------------------- ---------- ---------- ---
The Enum "OFN_FLAGS" contains some settings for your window. I have chosen some settings
from the windows 2000 environment
regards...
The Don
there are some properties, that will let you set specific settings for your Open File Button
This procedure here:
contains the default settings when you call the class.
--------------------------
Private Sub Class_Initialize()
m_strDefaultInitialDrive = "C:\" & vbNullChar
m_strDefaultOpenFileTitle = "Open" & vbNullChar
m_strDefaultSaveFileTitle = "Save As..." & vbNullChar
m_strDefaultFileType = "All Files" & vbNullChar & "*.*" & vbNullChar & vbNullChar
m_hexOpenFileFlags = OFN_ENABLE_SIZING Or OFN_ENABLE_EXPLORER_STYLE Or OFN_ENABLE_LONG_FILENAMES Or _
OFN_ENABLE_PATH_MUST_EXIST
m_hexSaveFileFlags = OFN_ENABLE_SIZING Or OFN_ENABLE_EXPLORER_STYLE Or OFN_ENABLE_LONG_FILENAMES Or _
OFN_ENABLE_PATH_MUST_EXIST
OFN_ENABLE_OVERWRITE_PROMP
m_lngDefaultWindowHandle = 0
m_lngDefaultExtensionIndex
m_lngDefaultEmptyStringFor
End Sub
--------------------------
The variable names are set to the same names as the properties you have.
--------------------------
To open a file use: OpenFileDialog
To save a file use: SaveFileDialog
--------------------------
--------------------------
The Enum "OFN_FLAGS" contains some settings for your window. I have chosen some settings
from the windows 2000 environment
regards...
The Don
ASKER
Bing