Link to home
Start Free TrialLog in
Avatar of JamesBing
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

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JamesBing
JamesBing

ASKER

what's Me.Hwnd?  I got a compile error of Invalid use of Me Keyword

Bing
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
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
Why don't you use CommonDialog ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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_EXTENSION = &H400
                            OFN_ENABLE_REFERENCED_FILELINKS = &H100000
                            OFN_ENABLE_LONG_FILENAMES = &H200000
                            OFN_ENABLE_OPEN_AS_READONLY = &H1
                            OFN_ENABLE_PATH_MUST_EXIST = &H800
                            OFN_ENABLE_FILE_MUST_EXIST = &H1000
                            OFN_ENABLE_HIDE_READONLY_FILES = &H4
                            OFN_ENABLE_OVERWRITE_PROMPT = &H2
                            OFN_ENABLE_HELP_BUTTON = &H10
                            OFN_ENABLE_IGNORE_SHARED_FILE_AWARENESS = &H4000
                            OFN_DISABLE_DIRECTORY_CHANGE = &H8
                            OFN_DISABLE_LONG_FILENAMES = &H40000
                            OFN_DISABLE_NETWORK_BUTTON = &H20000
                            OFN_DISABLE_READONLY_RETURN = &H8000
                            OFN_DISABLE_CREATION_OF_TESTFILE = &H10000
                            OFN_DISABLE_FILENAME_VALIDATION = &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_lngDefaultEmptyStringForFile As Long
                       
                       
                       

Public Property Let DefaultInitialDrive(ByVal strDefaultInitialDrive As String)
                       
                        Dim blnIsNotDrive As Boolean
                       
                       
   
    blnIsNotDrive = InStr(strDefaultInitialDrive, ":\") = 0
    If blnIsNotDrive Then
        strDefaultInitialDrive = "C:\" & vbNullChar
       
    End If
    m_strDefaultInitialDrive = strDefaultInitialDrive & vbNullChar
   
End Property

Public Property Get DefaultInitialDrive() As String
   
    DefaultInitialDrive = Replace(m_strDefaultInitialDrive, 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_strDefaultOpenFileTitle, 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_strDefaultSaveFileTitle, 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_strDefaultFileType, 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(ByVal 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 lngDefaultEmptyStringForFile As Long)
                       
                        Dim blnIndexIsLess As Boolean
                       
                       
                       
    If blnIndexIsLess = lngDefaultEmptyStringForFile < 256 Then
        lngDefaultEmptyStringForFile = 1024
   
    End If
    m_lngDefaultEmptyStringForFile = lngDefaultEmptyStringForFile
   
End Property

Public Property Get DefaultEmptyStringForFile() As Long
    DefaultEmptyStringForFile = m_lngDefaultEmptyStringForFile
   
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_PROMPT
    m_lngDefaultWindowHandle = 0
    m_lngDefaultExtensionIndex = 1
    m_lngDefaultEmptyStringForFile = 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_lngDefaultEmptyStringForFile) & vbNullChar
        .nMaxFile = Len(.lpstrFile)
        .lpstrFileTitle = Space(m_lngDefaultEmptyStringForFile) & 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_lngDefaultEmptyStringForFile) & vbNullChar
        .nMaxFile = m_lngDefaultEmptyStringForFile + 1
        .lpstrFileTitle = Space(m_lngDefaultEmptyStringForFile) & vbNullChar
        .nMaxFileTitle = m_lngDefaultEmptyStringForFile + 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

--------------------------------------------------------------------------
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.........

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_PROMPT
    m_lngDefaultWindowHandle = 0
    m_lngDefaultExtensionIndex = 1
    m_lngDefaultEmptyStringForFile = 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