Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2007

Tip: Looking for answers? Try searching our database.

List of files in dir

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MaxWedge426 - 27 Apr 2006 15:06 GMT
I used to have some code for this, but it's out of date now.  All I need is
to have Word or whatever agent create a list of the files in a specified
directory, and pop it into an Word document.  Simple enough, but it's beyond
me.  TIA
Jezebel - 27 Apr 2006 15:18 GMT
Posting here is easier than reading one single help topic?

>I used to have some code for this, but it's out of date now.  All I need is
> to have Word or whatever agent create a list of the files in a specified
> directory, and pop it into an Word document.  Simple enough, but it's
> beyond
> me.  TIA
MaxWedge426 - 28 Apr 2006 02:10 GMT
> Posting here is easier than reading one single help topic?
Well, I'd have to count that as hardly helpful, and just a tad toward the
snarky side - just a little.  I know how to do it with DOS, but that's not
needed.  If I KNEW the simple Help topic and where it was, I wouldn't dream
of disturbing Her Highness.  So, you had to comment and couldn't just scroll
down?
Ed - 28 Apr 2006 15:44 GMT
So you couldn't just read and scroll - you had to get "snarky" back?
Especially when typing "dir" in the VBA Help index yields "DIR Function" and
"DIR Function Example"?  And a search of the Word MVP FAQs would have turned
up the page garfield-n-odie gave you?  And a Google groups search of this
newsgroup would have turned up more code than you can use?

Not that I myself haven't been guilty of this and much more in my beginning
days, which weren't all that long ago.  I have found it is usually easier
for the good people who donate their time here helping others if the poster
has looked first and is having problems than to respond to "please write
this app for me for free."  The first demonstrates that you're trying to
learn; the second reminds me too much of my teenagers!

Jezebel has an incredible track record here; her willingness to help and
follow a thread through to great depths is well documented.  Knowing nothing
of you other than the words we read here, and reading through too many
requests for everything from homework answers to complete apps, a judgment
was made (one that appears to possibly be more accurate than not).  If you'd
like to join us in our pursuit of VBA excellence and share your knowledge
and experience, welcome!

Ed

>> Posting here is easier than reading one single help topic?
> Well, I'd have to count that as hardly helpful, and just a tad toward the
[quoted text clipped - 4 lines]
> scroll
> down?
Greg Maxey - 28 Apr 2006 16:03 GMT
Ed,

Personnally I wouldn't want to get too close or snugle up with Jezebel
with your idea (which I believe, but can't swear to in a court of law,
is seriously misconceived) that the being that refers to itself as
Jezebel is a she ;-).  I agree wholeheartedly with your assessment of
Jezebel's skills, however I think the name is more appropriate to warn
of the often waspish personality than as an indication of gender.
Ed - 28 Apr 2006 17:47 GMT
Again, assumptions reach out to snag me!  Thanks for the boost!
Ed

> Ed,
>
[quoted text clipped - 4 lines]
> Jezebel's skills, however I think the name is more appropriate to warn
> of the often waspish personality than as an indication of gender.
Dave Lett - 27 Apr 2006 15:29 GMT
Hi,

If you want only a folder, then you can use the following:

Option Explicit

Public Function fDirectoryListArray( _
   sPath As String, _
   sExtension As String) As Variant

Dim MyFile As String
Dim Counter As Long

'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000)
Counter = 0
'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$(sPath & "*" & sExtension)
Do While MyFile <> ""
   DirectoryListArray(Counter) = MyFile
   MyFile = Dir$
   Counter = Counter + 1
Loop

'Reset the size of the array without losing its values by using Redim
Preserve
If Counter <> 0 Then
   ReDim Preserve DirectoryListArray(Counter - 1)
Else
   ReDim Preserve DirectoryListArray(0)
End If

fDirectoryListArray = DirectoryListArray

End Function

Public Sub test()
Dim aDir As Variant
Dim iCount As Integer
Dim oDoc As Document
Set oDoc = Documents.Add
aDir = fDirectoryListArray(sPath:="C:\Documents and Settings\DLett\My
Documents\", sExtension:=".doc")
For iCount = 0 To UBound(aDir)
   oDoc.Range.InsertAfter aDir(iCount) & vbCrLf
Next iCount
End Sub

If you want an entire directory, then let me know. I think I have code for
that somewhere.

HTH,
Dave

>I used to have some code for this, but it's out of date now.  All I need is
> to have Word or whatever agent create a list of the files in a specified
> directory, and pop it into an Word document.  Simple enough, but it's
> beyond
> me.  TIA
MaxWedge426 - 28 Apr 2006 02:12 GMT
Thanks, Dave - That looks like just what I need.

Jack
Greg Maxey - 28 Apr 2006 16:06 GMT
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
Dave Lett - 28 Apr 2006 16:25 GMT
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

> Dave,
>
[quoted text clipped - 55 lines]
> Err.Clear
> End Function
Greg Maxey - 28 Apr 2006 16:48 GMT
Dave,

Yes for the most part my additions are gems gleaned from other stuff I
have read and picked up in the NGs as well.   I suppose for blinks
squirrels like me that there are very few nuts left to be found ;-)

Cheers.
muyBN - 27 Jan 2007 03:34 GMT
And I  bet that it was originally written by Jezebel. (;>)
Signature

Bryan

> Greg,
>
[quoted text clipped - 66 lines]
> > Err.Clear
> > End Function
garfield-n-odie [MVP] - 27 Apr 2006 19:00 GMT
See http://word.mvps.org/FAQs/MacrosVBA/InsertFileNames.htm .

> I used to have some code for this, but it's out of date now.  All I need is
> to have Word or whatever agent create a list of the files in a specified
> directory, and pop it into an Word document.  Simple enough, but it's beyond
> me.  TIA
MaxWedge426 - 28 Apr 2006 02:13 GMT
Very nice.  Much appreciated.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.