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
Set oFileSysObj = CreateObject("Scripting.FileSystemObject")
If Not oFileSysObj.FolderExists(txtPath.Text) Then
txtPath.Text = ""
For Each oDrive In oFileSysObj.Drives
lstFolders.AddItem oDrive.DriveLetter & ":"
Next
Else
Set oFolder = oFileSysObj.GetFolder(txtPath.Text)
For Each oSubFolder In oFolder.subfolders
lstFolders.AddItem oSubFolder.Name
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
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
Application.Documents.Add
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
WriteLine "File listing of " & txtPath.Text & vbLf & vbLf, True
WriteLine "File Name" & vbTab & "File Size" & vbLf, True
n = 1
For i = 0 To lstFolders.ListCount - 1
If Len(lstFolders.List(i)) > 4 And InStr(lstFolders.List(i), ".") = Len(lstFolders.List(i)) - 3 Then
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)
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)
txtPath.Text = txtPath.Text & lstFolders.Text & "\"
PopListBox
End Sub
Private Sub cmdUpLevel_Click()
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()
Unload Me
End Sub
|