I have created a list box in a Word VBA form. I want to populate the list box with all the filenames that appear in a specific directory (eg c:\my documents\precedents\""). I want to be able to then add files to this directory at any stage and know that the list box will automatically pick these up. The way I was taught to do this was to type each document name into VBA, (eg with me.listbox additem (""), etc) however there must be a faster and more efficient way. There are 30+ files in the directory so far and some 50 more to be added. I also want to be able to create a new document based on the document selected. The way I was taught to do this was along the lines of: select case me.listbox.text case "blank document" documents.add template:=strPath & "Normal.dot", newtemplate:=false etc end select Again, this is a long way about it. Is there a more efficient way? Would making a database listing all the filenames and their paths be a better way? Thank you! -- btcPosted from - http://www.officehelp.in
The following code in the Initialize() event for the userform will display a dialog that allows you to select the folder and will then populate a listbox named lstfiles on the UserForm with the files that are in that folder.: Dim MyPath As String Dim MyName As String 'let user select a path With Dialogs(wdDialogCopyFile) If .Display() <> -1 Then Exit Sub MyPath = .Directory End With 'strip quotation marks from path If Len(MyPath) = 0 Then Exit Sub If Asc(MyPath) = 34 Then MyPath = Mid$(MyPath, 2, Len(MyPath) - 2) End If 'get files from the selected path 'and insert them into the listbox lstfiles MyName = Dir$(MyPath & "*.*") Do While MyName <> "" lstfiles.AddItem MyName MyName = Dir Loop -- Hope this helps. Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis. Doug Robbins - Word MVP "btc" wrote in message news:btc.22hj76@NoSpamPleaze.com... > > I have created a list box in a Word VBA form. I want to populate the > list box with all the filenames that appear in a specific directory (eg > c:\my documents\precedents\""). I want to be able to then add files to > this directory at any stage and know that the list box will > automatically pick these up. > > The way I was taught to do this was to type each document name into > VBA, (eg with me.listbox additem (""), etc) however there must be a > faster and more efficient way. There are 30+ files in the directory so > far and some 50 more to be added. > > I also want to be able to create a new document based on the document > selected. The way I was taught to do this was along the lines of: > > select case me.listbox.text > case "blank document" > documents.add > template:=strPath & "Normal.dot", > newtemplate:=false > etc > end select > > Again, this is a long way about it. Is there a more efficient way? > > Would making a database listing all the filenames and their paths be a > better way? > > Thank you! > > > -- > btcPosted from - http://www.officehelp.in >