+ Reply to Thread
Results 1 to 7 of 7

Replace ability to select folder with a hardcoded directory

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Replace ability to select folder with a hardcoded directory

    How would I go about removing the prompt that allows my users to choose the directory in the code below so that the path is hard coded in the macro? The path I need to point this macro to is :Y:\CRW\Dal\Department\ProChef\Kitchen Cuttings\Cutting Sheets


    Sub HyperlinkFileList()
         'Macro purpose:  To create a hyperlinked list of all files in a user
         'specified directory, including file size and date last modified
         'NOTE:  The 'TextToDisplay' property (of the Hyperlink object) was added
         'in Excel 2000.  This code tests the Excel version and does not use the
         'Texttodisplay property if using XL 97.
         
        Dim fso As Object, _
        ShellApp As Object, _
        File As Object, _
        SubFolder As Object, _
        Directory As String, _
        Problem As Boolean, _
        ExcelVer As Integer
         
         'Turn off screen flashing
        Application.ScreenUpdating = False
         Columns("A:C").ClearContents
         'Create objects to get a listing of all files in the directory
        Set fso = CreateObject("Scripting.FileSystemObject")
         
         'Prompt user to select a directory
        Do
            Problem = False
            Set ShellApp = CreateObject("Shell.Application"). _
            Browseforfolder(0, "Please choose a folder", 0, "c:\\")
             
            On Error Resume Next
             'Evaluate if directory is valid
            Directory = ShellApp.self.Path
            Set SubFolder = fso.GetFolder(Directory).Files
            If Err.Number <> 0 Then
                If MsgBox("You did not choose a valid directory!" & vbCrLf & _
                "Would you like to try again?", vbYesNoCancel, _
                "Directory Required") <> vbYes Then Exit Sub
                Problem = True
            End If
            On Error GoTo 0
        Loop Until Problem = False
         
         'Set up the headers on the worksheet
        With ActiveSheet
            With .Range("A1")
                .Value = "Listing of all files in:"
                .ColumnWidth = 40
                 'If Excel 2000 or greater, add hyperlink with file name
                 'displayed.  If earlier, add hyperlink with full path displayed
                If Val(Application.Version) > 8 Then 'Using XL2000+
                    .Parent.Hyperlinks.Add _
                    Anchor:=.Offset(0, 1), _
                    Address:=Directory, _
                    TextToDisplay:=Directory
                Else 'Using XL97
                    .Parent.Hyperlinks.Add _
                    Anchor:=.Offset(0, 1), _
                    Address:=Directory
                End If
            End With
            With .Range("A2")
                .Value = "File Name"
                .Interior.ColorIndex = 15
                With .Offset(0, 1)
                    .ColumnWidth = 15
                    .Value = "Date Modified"
                    .Interior.ColorIndex = 15
                    .HorizontalAlignment = xlCenter
                End With
                With .Offset(0, 2)
                    .ColumnWidth = 15
                    .Value = "File Size (Kb)"
                    .Interior.ColorIndex = 15
                    .HorizontalAlignment = xlCenter
                End With
            End With
        End With
         
         'Adds each file, details and hyperlinks to the list
        For Each File In SubFolder
            If Not Excludes(Right(File.Path, 3)) = True Then
                With ActiveSheet
                     'If Excel 2000 or greater, add hyperlink with file name
                     'displayed.  If earlier, add hyperlink with full path displayed
                    If Val(Application.Version) > 8 Then 'Using XL2000+
                        .Hyperlinks.Add _
                        Anchor:=ActiveSheet.Range("A65536").End(xlUp).Offset(1, 0), _
                        Address:=File.Path, _
                        TextToDisplay:=File.Name
                    Else 'Using XL97
                        .Hyperlinks.Add _
                        Anchor:=ActiveSheet.Range("A65536").End(xlUp).Offset(1, 0), _
                        Address:=File.Path
                    End If
                     'Add date last modified, and size in KB
                    With .Range("A65536").End(xlUp)
                        .Offset(0, 1) = File.datelastModified
                        With .Offset(0, 2)
                            .Value = WorksheetFunction.Round(File.Size / 1024, 1)
                            .NumberFormat = "#,##0.0"
                        End With
                    End With
                End With
            End If
        Next
         
    End Sub
    Thanks!
    Last edited by dcgrove; 05-25-2010 at 02:14 PM. Reason: Holy engrish batman!!

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Replace ability to select folder with a hardcoded directory

    Maybe like this, but completely untested.

    The function Excludes is not defined.

    Option Explicit
    
    Sub HyperlinkFileList()
        ' Requires a reference to Microsoft Scripting Runtime
        
        Const sDir      As String = "Y:\CRW\Dal\Department\ProChef\Kitchen Cuttings\Cutting Sheets"
    
        Dim oFSO        As FileSystemObject
        Dim oFiles      As Files
        Dim oFile       As File
        Dim oFold       As Folder
        
        Dim iRow        As Long
        Dim iVer        As Long
    
        iVer = Val(Application.Version)
    
        Set oFSO = New FileSystemObject
        On Error Resume Next
        Set oFiles = oFSO.GetFolder(sDir).Files
        If Err.Number Then
            MsgBox "Invalid directory!"
            Exit Sub
        End If
        On Error GoTo 0
    
        Application.ScreenUpdating = False
        Columns("A:C").ClearContents
    
        With ActiveSheet
            With .Range("A1")
                .Value = "Listing of all files in:"
                .ColumnWidth = 40
    
                If iVer > 8 Then ' XL2000+
                    .Parent.Hyperlinks.Add Anchor:=.Offset(0, 1), Address:=sDir, TextToDisplay:=sDir
                Else
                    .Parent.Hyperlinks.Add Anchor:=.Offset(0, 1), Address:=sDir
                End If
            End With
    
            With .Range("A2")
                With .Resize(, 3)
                    .Value = Array("File Name", "Date Modified", "File Size (kB)")
                    .Interior.ColorIndex = 15
                End With
    
                With .Offset(, 1).Resize(, 2)
                    .ColumnWidth = 15
                    .HorizontalAlignment = xlCenter
                End With
            End With
        End With
    
        iRow = 1
        For Each oFile In oFiles
            iRow = iRow + 1
            If Not Excludes(Right(oFile.Path, 3)) = True Then
                With ActiveSheet
                    If iVer > 8 Then
                        .Hyperlinks.Add Anchor:=.Cells(iRow, "A"), Address:=oFile.Path, TextToDisplay:=oFile.Name
                    Else
                        .Hyperlinks.Add Anchor:=.Cells(iRow, "A"), Address:=oFile.Path
                    End If
    
                    .Cells(iRow, "B").Value = oFile.DateLastModified
                    With .Cells(iRow, "C")
                        .Value = Round(oFile.Size / 1024, 1)
                        .NumberFormat = "#,##0.0"
                    End With
                End With
            End If
        Next oFile
        
        Application.ScreenUpdating = True
    End Sub
    
    Function Excludes(sInp As String) As Boolean
    
    End Function
    Last edited by shg; 05-25-2010 at 02:46 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Replace ability to select folder with a hardcoded directory

    This is all
    Sub HyperlinkFileList()
        c00 = "Y:\CRW\Dal\Department\ProChef\Kitchen Cuttings\Cutting Sheets"
         c01 = "Listing of all files in:|File Name|File Size (Kb)|Date Modified"
         For Each fl In CreateObject("Scripting.FileSystemObject").getfolder(c00).Files
            c01 = c01 & vbCr & fl.Name & "|" & Replace(fl.Path, fl.Name, "") & "|" & fl.Size \ 1024 & "|" & fl.datelastmodified
        Next
        
        Columns("A:C").ClearContents
        Cells(1, 1).Resize(UBound(Split(c01, vbCr)) + 1) = WorksheetFunction.Transpose(Split(c01, vbCr))
        Columns(1).TextToColumns , 1, -4142, , False, False, False, False, True, "|"
        sq = UsedRange
        For j = 2 To UBound(sq)
            Hyperlinks.Add Cells(j, 1), sq(j, 2) & sq(j, 1), , , sq(j, 1)
        Next
    End Sub

  4. #4
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Replace ability to select folder with a hardcoded directory

    My apologies, I thought I had included it. The function Excludes is below.

    Function Excludes(Ext As String) As Boolean
         'Function purpose:  To exclude listed file extensions from hyperlink listing
         
        Dim X, NumPos As Long
         
         'Enter/adjust file extensions to EXCLUDE from listing here:
        X = Array("exe", "bat", "dll", "zip")
         
        On Error Resume Next
        NumPos = Application.WorksheetFunction.Match(Ext, X, 0)
        If NumPos > 0 Then Excludes = True
        On Error GoTo 0
         
    End Function

  5. #5
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Replace ability to select folder with a hardcoded directory

    SNB, thanks for the help! The code is throwing a "Run Time Error '13': Type Mismatch" error on this line.

     For j = 2 To UBound(sq)
    Also, is it possible for it to not have the dialog box come up that is asking to confirm whether or not I want to replace the contents of the cells?

    Thanks!

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Replace ability to select folder with a hardcoded directory

    Sub HyperlinkFileList()
      Application.displayalerts=false
    
      c00 = "Y:\CRW\Dal\Department\ProChef\Kitchen Cuttings\Cutting Sheets"
      c01 = "Listing of all files in:|File Name|File Size (Kb)|Date Modified"
      For Each fl In CreateObject("Scripting.FileSystemObject").getfolder(c00).Files
        c01 = c01 & vbCr & fl.Name & "|" & Replace(fl.Path, fl.Name, "") & "|" & fl.Size \ 1024 & "|" & fl.datelastmodified
      Next
        
      With sheets(1)
         .Columns("A:C").ClearContents
         .Cells(1, 1).Resize(UBound(Split(c01, vbCr)) + 1) = WorksheetFunction.Transpose(Split(c01, vbCr))
         .Columns(1).TextToColumns , 1, -4142, , False, False, False, False, True, "|"
         sq = .UsedRange
         For j = 2 To UBound(sq)
           Hyperlinks.Add .Cells(j, 1), sq(j, 2) & sq(j, 1), , , sq(j, 1)
         Next
      End With
    
      Application.displayalerts=True
    End Sub
    Last edited by snb; 05-25-2010 at 04:15 PM.

  7. #7
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Replace ability to select folder with a hardcoded directory

    SNB, now it is saying giving a "Runtime Error '424': Object Required" on the line below...

    Hyperlinks.Add Sheets(1).Cells(j, 1), sq(j, 2) & sq(j, 1), , , sq(j, 1)
    Any ideas why?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1