Put this code In a standard module:
Option Explicit
Sub runform()
UserForm1.Show
End Sub
This Is the code For your form:
Option Explicit
Private Sub UserForm_Initialize()
PopListBox
End Sub
Private Sub PopListBox()
Dim oFileSysObj As Object
Dim oFileSearch As Object
Dim oDrive As Object
Dim oFolder As Object
Dim oSubFolder As Object
Dim sFileName As String
Dim i As Integer
lstFolders.Clear 'clear list
Set oFileSysObj = CreateObject("Scripting.FileSystemObject")
If Not oFileSysObj.FolderExists(txtPath.Text) Then 'if no such folder
txtPath.Text = ""
For Each oDrive In oFileSysObj.Drives 'list drives
lstFolders.AddItem oDrive.DriveLetter & ":"
Next
Else 'if folder does exist
Set oFolder = oFileSysObj.GetFolder(txtPath.Text)
For Each oSubFolder In oFolder.subfolders
lstFolders.AddItem oSubFolder.Name 'add each subfolder
Next
Set oFileSearch = Application.FileSearch
With oFileSearch
.LookIn = txtPath.Text
.FileName = "*.*"
If .Execute > 0 Then
For i = 1 To .FoundFiles.Count
sFileName = oFileSysObj.getfilename(.FoundFiles(i))
lstFolders.AddItem sFileName 'add each file
Next i
End If
End With
End If
Set oFileSysObj = Nothing
Set oFileSearch = Nothing
Set oDrive = Nothing
Set oFolder = Nothing
Set oSubFolder = Nothing
End Sub
Private Sub cmdExport_Click()
Dim i As Integer, n As Integer
'Code for the export list button
' Create a new document for the file listing.
Application.Documents.Add
' Set tabs.
With Selection.ParagraphFormat.TabStops
.Add _
Position:=CentimetersToPoints(1), _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces
End With
With Selection.ParagraphFormat.TabStops
.Add _
Position:=CentimetersToPoints(14), _
Alignment:=wdAlignTabRight, _
Leader:=wdTabLeaderSpaces
End With
'Configure the appearance of your headers here
'you can add extra & vbLf to the end of lines for additional paragraph returns
WriteLine "File listing of " & txtPath.Text & vbLf & vbLf, True
' WriteLine "File listing of " & txtPath.Text & vbLf, True
WriteLine "File Name" & vbTab & "File Size" & vbLf, True
n = 1
For i = 0 To lstFolders.ListCount - 1
'will only return files with 3 letter file extentions
'to look for html files or files with 2 letter extentions
'change the number 3 at the end of the next line.
If Len(lstFolders.List(i)) > 4 And InStr(lstFolders.List(i), ".") = Len(lstFolders.List(i)) - 3 Then 'check if the name is "*.???"
WriteLine n & vbTab & lstFolders.List(i) & vbTab & _
Format(((FileLen(txtPath.Text & lstFolders.List(i)) / 1024) / 1024), "#.0") & "Mb" & vbLf, False
n = n + 1
End If
Next i
End Sub
Sub WriteLine(outputline As String, IsBold As Boolean)
'Formatting for your WriteLine or Headers
Selection.Font.Bold = IsBold
Selection.Font.Name = "Arial"
Selection.Font.Size = 10
Selection.TypeText outputline
End Sub
Private Sub lstFolders_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'Returns a folder listing and adds it to the listbox
txtPath.Text = txtPath.Text & lstFolders.Text & "\"
PopListBox
End Sub
Private Sub cmdUpLevel_Click()
'Code for the up a directory level button
Dim a As Integer
a = InStrRev(txtPath.Text, "\", Len(txtPath.Text) - 1, 1)
If a = 0 Then
txtPath.Text = ""
Else
txtPath.Text = Left(txtPath.Text, a)
End If
PopListBox
End Sub
Private Sub txtPath_Exit(ByVal Cancel As MSForms.ReturnBoolean)
PopListBox
End Sub
Private Sub cmdClose_Click()
'Code for the close button on the form
Unload Me
End Sub
|