Re: Populate List Box with Word doc names in a folder
- From: Steve C <SteveC@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 16 Sep 2008 08:33:01 -0700
Excellent, Jay! Borrowing from the code you gave me a link to (thank you
also, Bill Coan), I successfully modified mine as follows to do the trick.
Thanks for helping me learn something new!
Steve C.
Private Sub lstbxDivisions_Change()
'Runs whenever a check box in the 1st list box is selected
Dim MyPath As String
Dim MyName As String
Me.lstbxAvailDocs.Clear 'always clears 2nd list box before proceeding
For x = 0 To lstbxDivisions.ListCount - 1
If lstbxDivisions.Selected(x) = True Then 'a Division check box has been
selected
LastSelected = lstbxDivisions.List(x) 'captures Division number
'Insert into a document the names of all files in a selected folder
MyPath = "G:\Master Specifications\" & LastSelected & "\"
'Get files from the selected path and add them to 2nd list box
MyName = Dir$(MyPath & "*.doc") 'only looks for Word doc files
With lstbxAvailDocs
Do While MyName <> ""
MyName = Left(MyName, Len(MyName) - 4) 'removes .Doc from end of name
.AddItem MyName
MyName = Dir
Loop
End With
End If
"Jay Freedman" wrote:
Hi Steve,.
Nice thought, but the Open statement can't open a folder as if it was a text
file. Your code would bomb on that line and never go any further.
The function you need is the Dir function (short for "directory"). The way this
works is that you call it the first time with a pattern, consisting of a folder
path and a wildcard-specified filename. If the function returns a non-empty
string, that will be the filename of the first file that matches the pattern.
After that, you continue calling the Dir function without any parameter, getting
one name after another, until the result is an empty string.
An example of this use is in
http://www.word.mvps.org/FAQs/MacrosVBA/InsertFileNames.htm. To modify that code
to your use, replace the Selection.InsertAfter statement (for putting the
filename into a document) with your .AddItem statement to load the list box.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
- References:
- Populate List Box with Word doc names in a folder
- From: Steve C
- Re: Populate List Box with Word doc names in a folder
- From: Jay Freedman
- Populate List Box with Word doc names in a folder
- Prev by Date: Re: Need to find commands behind a ToolBarPopup control
- Next by Date: Sending Picture to behind text using Word Automation With C#
- Previous by thread: Re: Populate List Box with Word doc names in a folder
- Next by thread: Accessing a Word document source
- Index(es):
Relevant Pages
|