Re: Populate List Box with Word doc names in a folder

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.

.



Relevant Pages

  • Re: Opening filenames from a list in a spreadsheet
    ... XLS files in that folder. ... Optional InitialFolder As String) As String ... Dim SH As Shell32.Shell ... Dim FileName As String ...
    (microsoft.public.excel.programming)
  • Re: Can Excel search for workbook data in a Windows folder?
    ... dialog box to browse for folder; replace this portion with a pathname ... pszDisplayName As String ... Dim Directory As String ... PathAndName = CurrDir & FileName ...
    (microsoft.public.excel.misc)
  • Re: Path
    ... > How do you check if the path with the filename exists? ... 'see if the file exists in that folder ... Private Function FileExists(FileName As String) As Boolean ...
    (microsoft.public.vb.general.discussion)
  • Move a file
    ... I want to move a file from one folder to another folder. ... Dim PathSource As String ... Dim FileName As String ...
    (microsoft.public.excel.programming)
  • Re: filename changes during save
    ... >>> Whenever I am saving a file and have to navigate to the folder I want to ... >>> filename of the file I am trying to save changes to the filename that I ... > Hi Jay, ...
    (microsoft.public.windowsxp.general)