dcsimg
Results 1 to 2 of 2

Thread: Open/Save File Browser

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2016
    Posts
    28

    Open/Save File Browser

    In the main form, create 2 buttons: Command1 and Command2, then use the following code in the main form. This will open a File Browser...Command1 is just to look for files. Command2 is to Save a file:

    Code:
    Option Explicit
    
        Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenFilename As OPENFILENAME) As Long
        Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenFilename As OPENFILENAME) As Long
    
        Private Type OPENFILENAME
        
          lStructSize As Long          'The size of this struct (Use the Len function)
          hwndOwner As Long            'The hWnd of the owner window. The dialog will be modal to this window
          hInstance As Long            'The instance of the calling thread. You can use the App.hInstance here.
          lpstrFilter As String        'Use this to filter what files are showen in the dialog. Separate each filter with Chr$(0). The string also has to end with a Chr(0).
          lpstrCustomFilter As String  'The pattern the user has choosed is saved here if you pass a non empty string. I never use this one
          nMaxCustFilter As Long       'The maximum saved custom filters. Since I never use the lpstrCustomFilter I always pass 0 to this.
          nFilterIndex As Long         'What filter (of lpstrFilter) is showed when the user opens the dialog.
          lpstrFile As String          'The path and name of the file the user has chosed. This must be at least MAX_PATH (260) character long.
          nMaxFile As Long             'The length of lpstrFile + 1
          lpstrFileTitle As String     'The name of the file. Should be MAX_PATH character long
          nMaxFileTitle As Long        'The length of lpstrFileTitle + 1
          lpstrInitialDir As String    'The path to the initial path :) If you pass an empty string the initial path is the current path.
          lpstrTitle As String         'The caption of the dialog.
          flags As Long                'Flags. See the values in MSDN Library (you can look at the flags property of the common dialog control)
          nFileOffset As Integer       'Points to the what character in lpstrFile where the actual filename begins (zero based)
          nFileExtension As Integer    'Same as nFileOffset except that it points to the file extention.
          lpstrDefExt As String        'Can contain the extention Windows should add to a file if the user doesn't provide one (used with the GetSaveFileName API function)
          lCustData As Long            'Only used if you provide a Hook procedure (Making a Hook procedure is pretty messy in VB.
          lpfnHook As Long             'Pointer to the hook procedure.
          lpTemplateName As String     'A string that contains a dialog template resource name. Only used with the hook procedure.
          
        End Type
    
           Private Sub Command1_Click()
           
             Dim OpenFile As OPENFILENAME
             Dim lReturn As Long
             Dim sFilter As String
             OpenFile.lStructSize = Len(OpenFile)
             OpenFile.hwndOwner = Form1.hWnd
             OpenFile.hInstance = App.hInstance
             sFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0)
             OpenFile.lpstrFilter = sFilter
             OpenFile.nFilterIndex = 1
             OpenFile.lpstrFile = String(257, 0)
             OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
             OpenFile.lpstrFileTitle = OpenFile.lpstrFile
             OpenFile.nMaxFileTitle = OpenFile.nMaxFile
             OpenFile.lpstrInitialDir = "C:\"
             OpenFile.lpstrTitle = "Open Dialog Box by API"
             OpenFile.flags = 0
             lReturn = GetOpenFileName(OpenFile)
             If lReturn = 0 Then
                MsgBox "The User pressed the Cancel Button"
             Else
                MsgBox "The user Chose " & Trim(OpenFile.lpstrFile)
             End If
             
           End Sub
    
    Private Sub Command2_Click()
    
      Dim OpenFile As OPENFILENAME
      Dim lReturn As Long
      Dim sFilter As String
      OpenFile.lStructSize = Len(OpenFile)
      OpenFile.hwndOwner = Form1.hWnd
      OpenFile.hInstance = App.hInstance
      sFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0)
      OpenFile.lpstrFilter = sFilter
      OpenFile.nFilterIndex = 1
      OpenFile.lpstrFile = String(257, 0)
      OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
      OpenFile.lpstrFileTitle = OpenFile.lpstrFile
      OpenFile.nMaxFileTitle = OpenFile.nMaxFile
      OpenFile.lpstrInitialDir = "C:\"
      OpenFile.lpstrTitle = "Save Dialog Box by API"
      OpenFile.flags = 0
      lReturn = GetSaveFileName(OpenFile)
      If lReturn = 0 Then
         MsgBox "The User pressed the Cancel Button"
      Else
         MsgBox "The user Chose " & Trim(OpenFile.lpstrFile)
      End If
             
    End Sub

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    735

    Re: Open/Save File Browser

    Quote Originally Posted by ChildOfTheKing View Post
    In the main form, create 2 buttons: Command1 and Command2, then use the following code in the main form. This will open a File Browser...Command1 is just to look for files. Command2 is to Save a file:

    Code:
    Option Explicit
    
        Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenFilename As OPENFILENAME) As Long
        Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenFilename As OPENFILENAME) As Long
    
        Private Type OPENFILENAME
        
          lStructSize As Long          'The size of this struct (Use the Len function)
          hwndOwner As Long            'The hWnd of the owner window. The dialog will be modal to this window
          hInstance As Long            'The instance of the calling thread. You can use the App.hInstance here.
          lpstrFilter As String        'Use this to filter what files are showen in the dialog. Separate each filter with Chr$(0). The string also has to end with a Chr(0).
          lpstrCustomFilter As String  'The pattern the user has choosed is saved here if you pass a non empty string. I never use this one
          nMaxCustFilter As Long       'The maximum saved custom filters. Since I never use the lpstrCustomFilter I always pass 0 to this.
          nFilterIndex As Long         'What filter (of lpstrFilter) is showed when the user opens the dialog.
          lpstrFile As String          'The path and name of the file the user has chosed. This must be at least MAX_PATH (260) character long.
          nMaxFile As Long             'The length of lpstrFile + 1
          lpstrFileTitle As String     'The name of the file. Should be MAX_PATH character long
          nMaxFileTitle As Long        'The length of lpstrFileTitle + 1
          lpstrInitialDir As String    'The path to the initial path :) If you pass an empty string the initial path is the current path.
          lpstrTitle As String         'The caption of the dialog.
          flags As Long                'Flags. See the values in MSDN Library (you can look at the flags property of the common dialog control)
          nFileOffset As Integer       'Points to the what character in lpstrFile where the actual filename begins (zero based)
          nFileExtension As Integer    'Same as nFileOffset except that it points to the file extention.
          lpstrDefExt As String        'Can contain the extention Windows should add to a file if the user doesn't provide one (used with the GetSaveFileName API function)
          lCustData As Long            'Only used if you provide a Hook procedure (Making a Hook procedure is pretty messy in VB.
          lpfnHook As Long             'Pointer to the hook procedure.
          lpTemplateName As String     'A string that contains a dialog template resource name. Only used with the hook procedure.
          
        End Type
    
           Private Sub Command1_Click()
           
             Dim OpenFile As OPENFILENAME
             Dim lReturn As Long
             Dim sFilter As String
             OpenFile.lStructSize = Len(OpenFile)
             OpenFile.hwndOwner = Form1.hWnd
             OpenFile.hInstance = App.hInstance
             sFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0)
             OpenFile.lpstrFilter = sFilter
             OpenFile.nFilterIndex = 1
             OpenFile.lpstrFile = String(257, 0)
             OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
             OpenFile.lpstrFileTitle = OpenFile.lpstrFile
             OpenFile.nMaxFileTitle = OpenFile.nMaxFile
             OpenFile.lpstrInitialDir = "C:\"
             OpenFile.lpstrTitle = "Open Dialog Box by API"
             OpenFile.flags = 0
             lReturn = GetOpenFileName(OpenFile)
             If lReturn = 0 Then
                MsgBox "The User pressed the Cancel Button"
             Else
                MsgBox "The user Chose " & Trim(OpenFile.lpstrFile)
             End If
             
           End Sub
    
    Private Sub Command2_Click()
    
      Dim OpenFile As OPENFILENAME
      Dim lReturn As Long
      Dim sFilter As String
      OpenFile.lStructSize = Len(OpenFile)
      OpenFile.hwndOwner = Form1.hWnd
      OpenFile.hInstance = App.hInstance
      sFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0)
      OpenFile.lpstrFilter = sFilter
      OpenFile.nFilterIndex = 1
      OpenFile.lpstrFile = String(257, 0)
      OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
      OpenFile.lpstrFileTitle = OpenFile.lpstrFile
      OpenFile.nMaxFileTitle = OpenFile.nMaxFile
      OpenFile.lpstrInitialDir = "C:\"
      OpenFile.lpstrTitle = "Save Dialog Box by API"
      OpenFile.flags = 0
      lReturn = GetSaveFileName(OpenFile)
      If lReturn = 0 Then
         MsgBox "The User pressed the Cancel Button"
      Else
         MsgBox "The user Chose " & Trim(OpenFile.lpstrFile)
      End If
             
    End Sub
    And your question?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width