Results 1 to 13 of 13

Thread: [RESOLVED] Got dialog now what? /Final goal: folder contents 2 Excel SO Resolved

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    111

    Resolved [RESOLVED] Got dialog now what? /Final goal: folder contents 2 Excel SO Resolved

    Can't load vb on this computer so I can't use common dialog controls...which means I am stuck with the stuff in Excel and VBA.

    zaza posted something like this on another thread:

    VB Code:
    1. Sub getfolder()
    2.  
    3. Dim dlgOpen As FileDialog
    4. Dim strFolder As String
    5.     Set dlgOpen = Application.FileDialog( _
    6.         FileDialogType:=msoFileDialogFolderPicker)
    7.     With dlgOpen
    8.         .AllowMultiSelect = False
    9.         .Show
    10.     End With
    11. 'get the selected item into strFolder somehow
    12. End Sub

    This does get me the dialog box for folders (yay - figured that out) but I can't figure out how to get the selected folder into my string.

    I tried adding:
    strFolder = dlgOpen.selectedItems

    but that just gets me a compile error 'argument not optional'

    but since I don't know what arguments I need, that error is not so informative.

    -- then somehow using that folder name I am using Dir to get the folders and files to drop into the Excel Spreadsheet...but let me fuss out that part. For now.
    Last edited by Kantalope; Mar 23rd, 2006 at 03:34 PM.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    You were almost there on getting the path, here's some revised code that will give you the path, and also handles cases where the user presses Cancel.
    VB Code:
    1. Dim dlgOpen As FileDialog
    2. Dim strFolder As String
    3.    
    4.     Set dlgOpen = Application.FileDialog( _
    5.         FileDialogType:=msoFileDialogFolderPicker)
    6.    
    7.     strFolder = "No Folder Selected"
    8.    
    9.     With dlgOpen
    10.         .AllowMultiSelect = False
    11.         If .Show = -1 Then
    12.             strFolder = dlgOpen.SelectedItems(1)
    13.         End If
    14.     End With
    15.    
    16.     MsgBox strFolder
    17. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    111

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    Ah, knew it was something simple...gotta have the item number

    Now that would have been something useful to mention in the object browser.

    Does that mean that the things selected get tossed into an array?

    Thanks a ton...I am not going to mark this one as resolved yet. I might need more helps with getting the file info into the spreadsheet. But have to try first.

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    Quote Originally Posted by Kantalope
    Does that mean that the things selected get tossed into an array?
    Not quite, they are put into a collection rather than an array. The pluralization of SelectedItems is an indication that it is a collection ( a la Worksheets Vs. Worksheet).

    What exactly does your procedure need to do once the user has selected a directory? I can't really tell from your posts?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    111

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    The grand plan is to extract the folder contents: sub folders and files that contain our technical documents. The files need to be easy for our customers to browse so the tree is overly complicated and there are 500 some separate documents -- oh, and they need to be in two formats so double all that.

    I wanted to be able to grab all of the folders, or the particular folder someone was interested in working with a dump the contents into Excel...then they can sort or compare or whatever it is they want to do. Figured I would get the "file name","modified date","file location(path)"

    looks like dir can't help me like I thought...now I am leaning towards recursively searching with FSO...but am not married to the idea.

    Then I have to figure out how to parse the crummy reports that source safe spits out but that is for next week.

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    FSO is the way to go.
    Here's a recursive fuction that will, hopefully, give you what you need. I have also included a sample procedure showing how this function can be used.
    VB Code:
    1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    2. ' Comments:     Sample Proc showing how to use the FolderDetails
    3. '               function
    4. '
    5. ' Arguments:    None
    6. '
    7. ' Date          Developer       Action
    8. ' --------------------------------------------------------------
    9. ' 03/23/06      Declan Kenny    Initial version
    10. '
    11. Sub GetFileDetails()
    12. Dim sFolder As String
    13. Dim vFolderDetail As Variant
    14. Dim lRecord As Long
    15.    
    16.     'Get the folder to document
    17.     sFolder = GetFolder
    18.    
    19.     'Only continue if a folder has been chosen
    20.     If sFolder = "" Then
    21.         MsgBox "No Folder Selected", vbExclamation
    22.     Else
    23.         'Call the function to return the list of
    24.         'file properties
    25.         vFolderDetail = FolderDetails(sFolder, True)
    26.     End If
    27.    
    28.     'Output the results to a sheet in the current book
    29.     For lRecord = 1 To UBound(vFolderDetail, 2)
    30.         With ThisWorkbook.Worksheets(3)
    31.             .Cells(lRecord, 1) = vFolderDetail(1, lRecord)
    32.             .Cells(lRecord, 2) = vFolderDetail(2, lRecord)
    33.             .Cells(lRecord, 3) = vFolderDetail(3, lRecord)
    34.             .Cells(lRecord, 4) = vFolderDetail(4, lRecord)
    35.         End With
    36.     Next lRecord
    37. End Sub
    38.  
    39.  
    40. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    41. ' Comments:     This function returns the full path to a single
    42. '               folder chosen by the user
    43. '
    44. ' Arguments:    None
    45. '
    46. ' Date          Developer       Action
    47. ' --------------------------------------------------------------
    48. ' 03/23/06      Declan Kenny    Initial version
    49. '
    50. Function GetFolder() As String
    51. Dim dlgOpen As FileDialog
    52. Dim sFolder As String
    53.    
    54.     Set dlgOpen = Application.FileDialog( _
    55.         FileDialogType:=msoFileDialogFolderPicker)
    56.    
    57.     With dlgOpen
    58.         .AllowMultiSelect = False
    59.         If .Show = -1 Then
    60.             sFolder = dlgOpen.SelectedItems(1)
    61.         End If
    62.     End With
    63.    
    64.     GetFolder = sFolder
    65. End Function
    66.  
    67.  
    68. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    69. ' Comments:     This function returns an array of all files in
    70. '               a folder and (optionaly) is sub-folders.
    71. '               The following data is included in the array
    72. '                   File Name
    73. '                   Last Mod Date
    74. '                   File Path
    75. '                   File Type
    76. '               This function will be called recursively if
    77. '               sub-folders are included.
    78. '
    79. ' Arguments:    FolderPath          The full path to the folder
    80. '                                   being documented.
    81. '               IncludeSubFolders   Boolean to indicate if sub
    82. '                                   sub-folders should be included
    83. '
    84. ' Date          Developer       Action
    85. ' --------------------------------------------------------------
    86. ' 03/23/06      Declan Kenny    Initial version
    87. '
    88. Function FolderDetails(ByVal FolderPath As String, ByVal IncludeSubFolders As Boolean) As Variant
    89. Dim fsoFileSys As Scripting.FileSystemObject
    90. Dim oFolder As Scripting.Folder
    91. Dim oFile As Scripting.File
    92. Dim oSubFolder As Scripting.Folder
    93. Dim vReturn As Variant
    94. Dim vSubReturn As Variant
    95. Dim lSubRecord As Long
    96.    
    97.     'Set the initial size of the return array
    98.     ReDim vReturn(4, 1)
    99.    
    100.     'Create file system objects
    101.     Set fsoFileSys = New Scripting.FileSystemObject
    102.     Set oFolder = fsoFileSys.GetFolder(FolderPath)
    103.    
    104.     'Loop through each file un the folder
    105.     'adding its properties to the array
    106.     For Each oFile In oFolder.Files
    107.         vReturn(1, UBound(vReturn, 2)) = oFile.Name
    108.         vReturn(2, UBound(vReturn, 2)) = oFile.DateLastModified
    109.         vReturn(3, UBound(vReturn, 2)) = Replace(oFile.Path, oFile.Name, "")
    110.         vReturn(4, UBound(vReturn, 2)) = oFile.Type
    111.        
    112.         'Increase the size of the array by one
    113.         ReDim Preserve vReturn(4, UBound(vReturn, 2) + 1)
    114.     Next oFile
    115.    
    116.     'Remove the unused last record in the array
    117.     ReDim Preserve vReturn(4, UBound(vReturn, 2) - 1)
    118.    
    119.     'Do sub-folders need to be included?
    120.     If IncludeSubFolders Then
    121.        
    122.         'loop through each sub-folder
    123.         For Each oSubFolder In oFolder.SubFolders
    124.            
    125.             'Recursive call to this function
    126.             vSubReturn = FolderDetails(oSubFolder.Path, True)
    127.            
    128.             'Pass the values from the sub-call into the return array
    129.             For lSubRecord = 1 To UBound(vSubReturn, 2)
    130.                 ReDim Preserve vReturn(4, UBound(vReturn, 2) + 1)
    131.                 vReturn(1, UBound(vReturn, 2)) = vSubReturn(1, lSubRecord)
    132.                 vReturn(2, UBound(vReturn, 2)) = vSubReturn(2, lSubRecord)
    133.                 vReturn(3, UBound(vReturn, 2)) = vSubReturn(3, lSubRecord)
    134.                 vReturn(4, UBound(vReturn, 2)) = vSubReturn(4, lSubRecord)
    135.             Next lSubRecord
    136.         Next oSubFolder
    137.     End If
    138.    
    139.     'Return the array
    140.     FolderDetails = vReturn
    141.    
    142.     'Clean up
    143.     Set oSubFolder = Nothing
    144.     Set oFile = Nothing
    145.     Set oFolder = Nothing
    146.     Set fsoFileSys = Nothing
    147. End Function
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    111

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    HolyMoly DKenny It will take me all day to figure out all that code lol

    While I figure that all out, this is some stuff I cribbed off m$ but I keep getting an error 91 when I call the fso.getfolder

    The listbox part was just to test the results if I could ever get there; and the extra variables are remnants of the original code and I have not cleaned all those up yet

    VB Code:
    1. Sub loadfolder()
    2.  
    3. Dim strFolder1 As String
    4.    Dim nFolders As Long
    5.    Dim nfiles As Long
    6.    Dim lsize As Currency
    7.    Dim sDir As String
    8.    Dim sSrchString As String
    9.     Dim Print_File As String
    10.     Dim tfolder As Folder
    11.     Dim tfil As File
    12.     Dim filename As String
    13.  
    14. Dim dlgOpen As FileDialog
    15.     Set dlgOpen = Application.FileDialog( _
    16.         FileDialogType:=msoFileDialogFolderPicker)
    17.        
    18.         strFolder1 = "no folder selected"
    19.        
    20.     With dlgOpen
    21.         .InitialFileName = "c:\"
    22.         .Title = "Select Folder to Load"
    23.         .AllowMultiSelect = False
    24.         If .Show = -1 Then
    25.             strFolder1 = dlgOpen.SelectedItems(1)
    26.             End If
    27.     End With
    28.  
    29.  
    30. 'Part 2 get directory info
    31.  
    32. Dim fld As Folder
    33. Dim fso As FileSystemObject
    34.  
    35. Set fld = fso.getfolder(strfolder1)
    36.  
    37. filename = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or _
    38.                     vbHidden Or vbSystem Or vbReadOnly)
    39.  
    40.     While Len(filename) <> 0
    41.     nfiles = nfiles + 1
    42.     UserForm1.ListBox1.AddItem fso.BuildPath(fld.Path, filename) 'listbox test
    43.     filename = Dir() 'getting next file
    44.     DoEvents
    45.     Wend
    46.     '''''
    47.  
    48. 'This part drops the file name into the spreadsheet
    49.        'Print_File = fsoFiles(strFolder1, "*.*", nFolders, nfiles)
    50.    'Range("a1").Select
    51.    'Counter = 1
    52.    'Do While Len(Print_File) > 0
    53.    'If Print_File <> "vssver.scc" Then
    54.     'Worksheets(ActiveSheet.Name).Cells(Counter, 1).Value = _
    55.            'Print_File
    56.     'End If  'Dont load the folder option file - no one cares
    57.        'Print_File = Dir()
    58.        'Counter = Counter + 1
    59.    'Loop
    60.  
    61. End Sub

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    So, I'm really bored today....

    Here's a revised version that is not limitted to those 4 file properties. In this version you can pass an array of properites to the function and get the values for each.

    Again, I have a sample proc that shows how to use the function.
    VB Code:
    1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    2. ' Comments:     Sample Proc showing how to use the FolderDetails
    3. '               function
    4. '
    5. ' Arguments:    None
    6. '
    7. ' Date          Developer       Action
    8. ' --------------------------------------------------------------
    9. ' 03/23/06      Declan Kenny    Initial version
    10. '
    11. Sub GetFileDetails()
    12. Dim sFolder As String
    13. Dim vFolderDetail As Variant
    14. Dim lRecord As Long
    15. Dim lField As Long
    16.    
    17.     'Get the folder to document
    18.     sFolder = GetFolder
    19.    
    20.     'Only continue if a folder has been chosen
    21.     If sFolder = "" Then
    22.         MsgBox "No Folder Selected", vbExclamation
    23.     Else
    24.         'Call the function to return the list of
    25.         'file properties
    26.         vFolderDetail = FolderDetails(sFolder, True, Array("Name", "Type", "DATELASTMODIFIED", "Size", "Path", "ShortPath"), True)
    27.     End If
    28.    
    29.     'Output the results to a sheet in the current book
    30.     For lRecord = LBound(vFolderDetail, 2) To UBound(vFolderDetail, 2)
    31.         For lField = LBound(vFolderDetail) To UBound(vFolderDetail)
    32.             ActiveWorkbook.Worksheets(3).Cells(lRecord + 1, lField + 1) = vFolderDetail(lField, lRecord)
    33.         Next lField
    34.     Next lRecord
    35. End Sub
    36.  
    37. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    38. ' Comments:     This function returns the full path to a single
    39. '               folder chosen by the user
    40. '
    41. ' Arguments:    None
    42. '
    43. ' Date          Developer       Action
    44. ' --------------------------------------------------------------
    45. ' 03/23/06      Declan Kenny    Initial version
    46. '
    47. Function GetFolder() As String
    48. Dim dlgOpen As FileDialog
    49. Dim sFolder As String
    50.    
    51.     Set dlgOpen = Application.FileDialog( _
    52.         FileDialogType:=msoFileDialogFolderPicker)
    53.    
    54.     With dlgOpen
    55.         .AllowMultiSelect = False
    56.         If .Show = -1 Then
    57.             sFolder = dlgOpen.SelectedItems(1)
    58.         End If
    59.     End With
    60.    
    61.     GetFolder = sFolder
    62. End Function
    63.  
    64.  
    65. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    66. ' Comments:     This function returns an array of properties for
    67. '               all files in a folder and, optionaly, it sub-folders.
    68. '               This function will be called recursively if
    69. '               sub-folders are included.
    70. '
    71. ' Arguments:    FolderPath          The full path to the folder
    72. '                                   being documented.
    73. '
    74. '               IncludeSubFolders   Boolean to indicate if
    75. '                                   sub-folders should be included
    76. '
    77. '               FileProps           An array of file properties to
    78. '                                   be returned
    79. '
    80. '               FirstCall           A boolean used to indicate that
    81. '                                   the function is being called
    82. '                                   originally or recursively
    83. '
    84. ' Date          Developer       Action
    85. ' --------------------------------------------------------------
    86. ' 03/23/06      Declan Kenny    Initial version
    87. '
    88. Function FolderDetails(ByVal FolderPath As String, _
    89.                         ByVal IncludeSubFolders As Boolean, _
    90.                         ByRef FileProps As Variant, _
    91.                         Optional ByVal FirstCall As Boolean = True) As Variant
    92.  
    93. Dim fsoFileSys As Scripting.FileSystemObject
    94. Dim oFolder As Scripting.Folder
    95. Dim oFile As Scripting.File
    96. Dim oSubFolder As Scripting.Folder
    97. Dim vReturn As Variant
    98. Dim vSubReturn As Variant
    99. Dim lPropNum As Long
    100. Dim sPropValue As String
    101. Dim lSubRecord As Long
    102.    
    103.     'Create file system objects
    104.     Set fsoFileSys = New Scripting.FileSystemObject
    105.     Set oFolder = fsoFileSys.GetFolder(FolderPath)
    106.    
    107.     'Set the initial size of the return array
    108.     ReDim vReturn(LBound(FileProps) To UBound(FileProps), 0)
    109.    
    110.     'Add the Property headers to the return array
    111.     'Only if they have not yet been added
    112.     If FirstCall Then
    113.         For lPropNum = LBound(FileProps) To UBound(FileProps)
    114.             vReturn(lPropNum, 0) = FileProps(lPropNum)
    115.         Next lPropNum
    116.     End If
    117.  
    118.     'Loop through each file in the folder
    119.     'adding its properties to the array
    120.     For Each oFile In oFolder.Files
    121.         'Increase the size of the array by one
    122.         ReDim Preserve vReturn(LBound(FileProps) To UBound(FileProps), UBound(vReturn, 2) + 1)
    123.        
    124.         'Loop through the properties, adding each to the return array
    125.         For lPropNum = LBound(FileProps) To UBound(FileProps)
    126.            
    127.             'Determine which file property to add
    128.             Select Case UCase(FileProps(lPropNum))
    129.                 Case "DATECREATED":         sPropValue = oFile.DateCreated
    130.                 Case "DATELASTACCESSED":    sPropValue = oFile.DateLastAccessed
    131.                 Case "DATELASTMODIFIED":    sPropValue = oFile.DateLastModified
    132.                 Case "DRIVE":               sPropValue = oFile.Drive
    133.                 Case "NAME":                sPropValue = oFile.Name
    134.                 Case "PARENTFOLDER":        sPropValue = oFile.ParentFolder
    135.                 Case "PATH":                sPropValue = Replace(oFile.Path, oFile.Name, "")
    136.                 Case "SHORTNAME":           sPropValue = oFile.ShortName
    137.                 Case "SHORTPATH":           sPropValue = oFile.ShortPath
    138.                 Case "SIZE":                sPropValue = oFile.Size
    139.                 Case "TYPE":                sPropValue = oFile.Type
    140.                 Case Else:                  sPropValue = ""
    141.             End Select
    142.            
    143.             'Add the propert to the array
    144.             vReturn(lPropNum, UBound(vReturn, 2)) = sPropValue
    145.         Next lPropNum
    146.     Next oFile
    147.    
    148.     'Do sub-folders need to be included?
    149.     If IncludeSubFolders Then
    150.        
    151.         'loop through each sub-folder
    152.         For Each oSubFolder In oFolder.SubFolders
    153.            
    154.             'Recursive call to this function
    155.             vSubReturn = FolderDetails(oSubFolder.Path, True, FileProps, False)
    156.            
    157.             'Pass the values from the sub-call into the return array
    158.             For lSubRecord = 1 To UBound(vSubReturn, 2)
    159.                 ReDim Preserve vReturn(LBound(FileProps) To UBound(FileProps), UBound(vReturn, 2) + 1)
    160.                 For lPropNum = LBound(FileProps) To UBound(FileProps)
    161.                     vReturn(lPropNum, UBound(vReturn, 2)) = vSubReturn(lPropNum, lSubRecord)
    162.                 Next lPropNum
    163.             Next lSubRecord
    164.         Next oSubFolder
    165.     End If
    166.    
    167.     'Return the array
    168.     FolderDetails = vReturn
    169.    
    170.     'Clean up
    171.     Set oSubFolder = Nothing
    172.     Set oFile = Nothing
    173.     Set oFolder = Nothing
    174.     Set fsoFileSys = Nothing
    175. End Function
    Last edited by DKenny; Mar 23rd, 2006 at 03:12 PM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    Try copying my new code (post #8) into a fresh workbook and then run the "GetFileDetails" procedure. Let me know if this is what you want.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  10. #10
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    Kantalope
    I had a typo in the code in pst #8. I have now corrected it.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    111

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    Oh my god....that is so what I wanted to do.

    You can't tell my boss that you did this in a few minutes and on a lark...I planned on goofing off for the rest of the day learning this stuff.

    And the most referenced way to do this on the web was several modules deep and api calls and you just spit this stuff out.

    Awesome DKenny...and the typo didn't seem to effect it but I will cut and paste again. That I know how to do.

  12. #12
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    Quote Originally Posted by Kantalope
    I planned on goofing off for the rest of the day learning this stuff.
    That's mainly how I learned too.

    I won't tell youre boss if you won't tell mine
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    111

    Thumbs up Re: [RESOLVED] Got dialog now what? /Final goal: folder contents 2 Excel SO Resolved

    That code should go to the code bank...I can't believe more folks don't need to analyze folder contents...


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