Re: List of files in dir by Dave
Dave
Fri Apr 28 10:25:23 CDT 2006
Greg,
Looks good, but I have to confess that the bulk of the routine is not mine;
as I'm sure you noticed, I got a lot of the basic stuff from the Word MVP's
site. I simply turned it into a function because I've found that I reuse
that quite a bit. I do like the added function of the dialog box (much more
dynamic). I didn't have a good reason for not including that in my basic
routine.
Dave
"Greg Maxey" <gmaxey@mvps.org> wrote in message
news:1146236768.014336.258670@v46g2000cwv.googlegroups.com...
> Dave,
>
> I took the liberty to abbreviate your code a little and add a Directory
> picker function and extension input box:
>
> Public Sub CreateFileList()
> Dim aDir As Variant
> Dim i As Integer
> Dim oDoc As Document
> Dim PathToUse As String
> Dim pExt As String
> PathToUse$ = GetPathToUse
> If PathToUse = "No selection" Or PathToUse = "Error" Then Exit Sub
> pExt = InputBox("Enter file extension or '*' for all file types.", _
> "Extension", "*")
> Set oDoc = Documents.Add
> aDir = fDirList(PathToUse, "." & pExt)
> For i = 0 To UBound(aDir)
> oDoc.Range.InsertAfter aDir(i) & vbCrLf
> Next i
> End Sub
> Public Function fDirList(sPath As String, sExt As String) As Variant
> Dim MyFile As String
> Dim Counter As Long
> 'Create a dynamic array variable declare a liberal initial size
> Dim DirListArray() As String
> ReDim DirListArray(0) As String
> Counter = 0
> 'Loop through all the files in the directory by using Dir$ function
> MyFile = Dir$(sPath & "*" & sExt)
> Do While MyFile <> ""
> DirListArray(Counter) = MyFile
> MyFile = Dir$
> Counter = Counter + 1
> ReDim Preserve DirListArray(Counter)
> Loop
> fDirList = DirListArray
> End Function
> Private Function GetPathToUse() As Variant
> On Error GoTo Handler
> 'Get the folder containing the files. Note - The "Copy Dialog" is used
> to
> 'to display the "open" option
> With Dialogs(wdDialogCopyFile)
> If .Display <> 0 Then
> GetPathToUse = .Directory
> Else
> GetPathToUse = "No selection"
> Exit Function
> End If
> End With
> If Left(GetPathToUse, 1) = Chr(34) Then
> GetPathToUse = Mid(GetPathToUse, 2, Len(GetPathToUse) - 2)
> End If
> Exit Function
> Handler:
> GetPathToUse = "Error"
> Err.Clear
> End Function
>