Results 1 to 8 of 8

Thread: how to browse file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71

    how to browse file

    Hello, I want my user to browse file, looking for pictures, then i need the path to this file to copy it in a document, but this is another part of the program.

    So, how can i allow the user to browse the file, to select a document, and then get the path

    thanks

    writelearner

  2. #2
    New Member
    Join Date
    Sep 2002
    Location
    Fantasia, Castle Donnington 1993
    Posts
    8
    Try looking at the Common Dialog Control. This should be what you are looking for.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    actually, i did, but what i need is to get the path from the text box of the common form, and i don't know its name

  4. #4
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    You can do this in Word, but not in Excel. All you can do with Excel dialogs is show them and let them do whatever they do. To get a file name in Excel, you will need to create a UserForm (on the VBA menu select Insert then UserForm). Now you have to add the Common Dialog Control (make sure the UserForm object is visible and not just the code, if it isn't select View then Object. On the VBA menu select Tools, the Additional Controls, then check the box for Microsoft Common Dialog Control.) Now you should have the CommonDialog on your tool box (if you don't see the toolbox, click View then Toolbox). Drag the CommonDialog to the UserForm. Now you can add this code to the UserForm.

    VB Code:
    1. Option Explicit
    2.  
    3. Public strPictureFileSelected As String
    4.  
    5. Private Sub UserForm_Activate()
    6.    
    7.     On Error GoTo ErrorOut
    8.    
    9.     ' Hide the user form.
    10.     UserForm1.Hide
    11.        
    12.     With CommonDialog1
    13.        
    14.         ' Set to error out if user cancels.
    15.         .CancelError = True
    16.        
    17.         ' Set the dialog title.
    18.         .DialogTitle = "Select Picture"
    19.        
    20.         ' Set dialog filters.
    21.         ' Everything before "|" is what the user sees.
    22.         ' Everything after "|" is the file types the dialog will show.
    23.         .Filter = "Pictures (*.bmp;*.gif;*.jpg)|*.bmp;*.gif;*.jpg|All Files|*.*"
    24.        
    25.         ' Show the dialog.
    26.         .ShowOpen
    27.        
    28.         ' Get the selected file name.
    29.         strPictureFileSelected = .Filename
    30.    
    31.     End With
    32.    
    33.     ' Here is the selected file name.
    34.     MsgBox strPictureFileSelected
    35.    
    36.     Exit Sub
    37.    
    38. ErrorOut:
    39.     ' User pressed cancel or exited dialog.
    40.     strPictureFileSelected = ""
    41.  
    42. End Sub
    Now strPictureFileSelected has the selected file name and you can do whatever you want with it.
    Last edited by WorkHorse; Jun 11th, 2003 at 11:06 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    Thanks for your help, and sorry for the late reply.
    My computer crashed down and if you have an answer to the fatal error threat, your my super hero and i love you from the bottom of my heart (si,si, je le jure)

    but concerning the common dialog, i now have it on my tool box, but i can't insert it on my form.
    it says it is not properly referenced. ???

    got a clue for this one

    thanks very much anyway

  6. #6
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    VB Code:
    1. '---- File constants
    2. Public Const mMax_Path = 512
    3.  
    4. Public Type mOpenFilename
    5.     lStructSize As Long
    6.     hwndOwner As Long
    7.     hInstance As Long
    8.     lpstrFilter As String
    9.     lpstrCustomFilter As String
    10.     nMaxCustFilter As Long
    11.     nFilterIndex As Long
    12.     lpstrFile As String
    13.     nMaxFile As Long
    14.     lpstrFileTitle As String
    15.     nMaxFileTitle As Long
    16.     lpstrInitialDir As String
    17.     lpstrTitle As String
    18.     Flags As Long
    19.     nFileOffset As Integer
    20.     nFileExtension As Integer
    21.     lpstrDefExt As String
    22.     lCustData As Long
    23.     lpfnHook As Long
    24.     lpTemplateName As String
    25. End Type
    26.  
    27. '---- common dialog stuff :)
    28. Public Declare Function apiGetOpenFilename Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pmOpenFilename As mOpenFilename) As Long
    29. Public Declare Function apiGetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pmOpenFilename As mOpenFilename) As Long
    30. Public Declare Function apiGetChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (pmChooseColor As mChooseColor) As Long
    31.  
    32.  
    33. '---- Selects a/several file(s) using common dialogs
    34. Public Function FileSelection(ByVal strInitDir As String, ByVal lngFlag As Long, ByVal strFilter As String, ByVal lngFilterIndex As Long, ByVal blnOpen As Boolean, Optional strTitle, Optional strFileExtension, Optional lngOwnerHwnd) As String
    35. '---- Selects one or more filenames
    36. '---- Requires
    37. '----   inital directory to open in
    38. '----   flags for opening
    39.  
    40.  
    41. '---- usual flags are :
    42. '----   &H200000 - use long filenames
    43. '----   &H80000 - Explorer type window
    44. '----   &H200 - multi select
    45. '----   &H4 - hide read only option on box - v useful
    46. '----   &H8 - force same dir as when opened
    47. '----   &H1000 - file must exist - useful for opening
    48. '---- usual I use : &H280004
    49.  
    50. '---- Returns a string holding either :
    51. '----   nothing
    52. '----   path and filename
    53. '----   path chr$(0) filenames separated by chr$(0)
    54. '---- NOTE : current max chars = 257 ... I think expand if neccessary
    55.  
    56.     Dim strTemp As String
    57.     Dim lngReturn As Long, lngP As Long, lngO As Long
    58.     Dim cOpenFilename As mOpenFilename
    59.  
    60.     On Error Resume Next
    61.  
    62.     With cOpenFilename
    63.  
    64. '---- Default values according to another developer
    65.         .lStructSize = Len(cOpenFilename)
    66.         .hInstance = 0
    67.         .nFilterIndex = 1
    68.         .nFileOffset = 0
    69.         .lpstrFile = String(5000, 0)
    70.         .nMaxFile = Len(.lpstrFile) - 1
    71.         .lpstrFileTitle = .lpstrFile
    72.         .nMaxFileTitle = .nMaxFile
    73.  
    74.         If Not IsMissing(strFileExtension) Then .lpstrDefExt = strFileExtension
    75.  
    76.  
    77. '---- Bits like the common dialog control
    78.         If blnOpen Then
    79.             .lpstrTitle = "Open a file..."
    80.         Else
    81.             .lpstrTitle = "Save file as..."
    82.         End If
    83.         If Not IsMissing(strTitle) Then .lpstrTitle = strTitle
    84.  
    85. '---- messing
    86. '----   default filter
    87.  
    88. '---- each filter is separated by a character of 0 - Name - filter - name filter (etc..)
    89. '---- example :
    90. '        .lpstrFilter = "All Files" & Chr$(0) & "*.*" & Chr$(0) & "Text Files" & Chr$(0) & "*.txt;*.csv"
    91. '---- replace filter with the selection chosen by the programmer...
    92.         .lpstrFilter = "All Files (*.*)" & Chr$(0) & "*.*"
    93.  
    94.         lngO = 1
    95.         lngP = InStr(1, strFilter, "|")
    96.         If lngP > 0 Then
    97.             strTemp = ""
    98.             Do Until lngP = 0
    99.                 strTemp = strTemp & IIf(Len(strTemp) > 0, Chr$(0), "") & Mid$(strFilter, lngO, lngP - lngO)
    100.                 lngO = lngP + 1
    101.                 lngP = InStr(lngP + 1, strFilter, "|")
    102.             Loop
    103.             strTemp = strTemp & Chr$(0) & Right$(strFilter, Len(strFilter) - lngO + 1)
    104.         Else
    105.             strTemp = strFilter
    106.         End If
    107.         .lpstrFilter = strTemp
    108.  
    109.         .Flags = lngFlag
    110.  
    111.         .hwndOwner = 0
    112.         If Not IsMissing(lngOwnerHwnd) Then .hwndOwner = lngOwnerHwnd
    113. '---- remark out the next line - or amend it as applicable
    114.         If .hwndOwner = 0 Then .hwndOwner = Application.hWndAccessApp
    115.  
    116.         .lpstrInitialDir = strInitDir
    117.  
    118.     End With
    119.  
    120. '---- is the dialog box an open or save?
    121.     If blnOpen Then
    122.         lngReturn = apiGetOpenFilename(cOpenFilename)
    123.     Else
    124.         lngReturn = apiGetSaveFileName(cOpenFilename)
    125.     End If
    126.  
    127. '---- send back the selected file(s)
    128.     If lngReturn = 0 Then
    129.         FileSelection = ""
    130.     Else
    131.         FileSelection = RemoveNonPChars(cOpenFilename.lpstrFile)
    132.     End If
    133.  
    134. End Function
    135.  
    136. Public Function RemoveNonPChars(ByVal strText As String) As String
    137. '---- gets rid of the extra chr$(0)'s in the text
    138. '---- by looking for two chr$(0)'s together (only happens at the end...)
    139. '---- NOTE : only removes those at the end of the string - for multi means that you can get the filenames...
    140.     Dim lngP As Long
    141.  
    142.     On Error Resume Next
    143.  
    144.     RemoveNonPChars = " "
    145.     If Len(strText) = 0 Then Exit Function
    146.     lngP = InStr(1, strText, Chr$(0) & Chr$(0))
    147.     If lngP > 1 Then RemoveNonPChars = Left$(strText, lngP - 1)
    148.     If Not Err.Number = 0 Then
    149.         RemoveNonPChars = " "
    150.         Err.Clear
    151.     End If
    152. End Function

    Paste into a module

    Writen based on someone elses code which seems to be based on MSDN coding on common dialog dlls
    Any probs - lemme know


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    ok, your code is very tought, i don't understand everythings and got lots of error. don't have a clue on how to resolve them, maybe the best is to code my own form, but i don't have a clue of how to show all the files contained in a folder on a user form.

    this sad, because all i need is to get a code like

    variable = insertpicture.textbox2.value

    and hop no more troubles

    Ha why things have to be complicated

  8. #8
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Hi

    What errors?
    I have that code in a module and it works fine, opens a standard windows box.


    If you have to, use Dir$ function to get names and display ina list box or something.
    VB Code:
    1. strFilename=Dir$(strPath & "*.*",63)
    2. strFullPath=strPath & strFilename
    3. Do until len(strFilename)=0
    4.   if not (strfilename="." or strfilename="..") then
    5. '---- Add to list (you decide) with full path recorded somewhere and filename displayed
    6.     lst.add strfullpath,strfilename
    7.   end if
    8.  
    9. '---- blank file name as otherwise it won't be set to zero length and you'd endlessly loop
    10.   strfilename=""
    11.   strFilename=Dir$
    12. loop

    Up to you - investigate all the options.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

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



Click Here to Expand Forum to Full Width