View Full Version : How to List Contents Of A Folder In A ComboBox
IRduranni
12-31-2013, 05:47 AM
Hi,
:help I have a Form in Word with 3 ComboBox's and a CommandButton.
I would like ComboBox1 to show the contents of a folder (which contains folders).
I would like ComboBox2 to show the contents of the folder selected in ComboBox1 (which also contains folders).
I would then like ComboBox3 to show the contents of the folder selected in ComboBox2 (which contains Word Documents).
Once a choice is made in ComboBox3, i would like to click on the CommandButton and have it run the contents of that file (Word Document) with Microsoft Word
Can someone please help me with this: pray2: , i would be very grateful :yes.
Many Thanks
IRduranni
mrojas
12-31-2013, 02:17 PM
Here's a chunk of code that would add items to a combo box. These items correspond to a folder, meaning file names in a folder. This, I think, will get you started.
Private Sub btnListOfFolders_Click()
Dim objFileSystem, objFolder, objFile, objFileCollection, objFolderCollection
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFileSystem.GetFolder("c:\data\")
Set objFileCollection = objFolder.Files
For Each objFile In objFileCollection
Me.cboDirAndFiles.AddItem objFile.Name
Next
End Sub
IRduranni
12-31-2013, 08:54 PM
hello majos thank for helping =)
i got this source code when googeling but don't exactly know how to use and its seem not support VB 2008 hope u can help he =)
Option Explicit
'MAKE SURE YOU CAHNGE THIS CONSTANT TO A VALID PATH!!!
Const strStartFldr AsString="C:\Documents and Settings\Killian\"
Dim fso, froot, fldr, f
PrivateSub UserForm_Initialize()
Set fso = CreateObject("Scripting.FileSystemObject")
Set froot = fso.GetFolder(strStartFldr)
ForEach fldr In froot.SubFolders
ComboBox1.AddItem fldr.Name
Next
ComboBox1.ListIndex =0
EndSub
PrivateSub ComboBox1_Change()
ComboBox2.Clear
Set froot = fso.GetFolder(strStartFldr & ComboBox1.Text)
ForEach fldr In froot.SubFolders
ComboBox2.AddItem fldr.Name
Next
If ComboBox2.ListCount >0Then ComboBox2.ListIndex =0
EndSub
PrivateSub ComboBox2_Change()
ComboBox3.Clear
Set froot = fso.GetFolder(strStartFldr & ComboBox1.Text &"\"& ComboBox2.Text)
ForEach f In froot.Files
If UCase(Right(f.Name,3))="DOC"Then
ComboBox3.AddItem f.Name
EndIf
Next
If ComboBox3.ListCount >0Then ComboBox3.ListIndex =0
EndSub
PrivateSub CommandButton1_Click()
With ActiveDocument.Bookmarks("bkTest")
.Range.InsertFile (strStartFldr & ComboBox1.Text &"\"& ComboBox2.Text &"\"& ComboBox3.Text)
EndWith
Unload Me
EndSub
PrivateSub UserForm_Terminate()
Set f =Nothing
Set fldr =Nothing
Set froot =Nothing
Set fso =Nothing
EndSub
Option Explicit
'MAKE SURE YOU CAHNGE THIS CONSTANT TO A VALID PATH!!!
Const strStartFldr As String = "F:\Desktop\" '"C:\Documents and Settings\Killian\"
Private Sub CommandButton1_Click()
Dim FName A String
FName = GetFileToInsert
If FName = "Canceled" Then
Exit Sub
Else
ActiveDocument.Bookmarks("bkTest").Range.InsertFile (FName)
End If
Unload Me
End Sub
For some reason, I can't get the .Filters.Add method to work on my box. Try uncommenting it before you test this function.
Function GetFileToInsert()
'Returns "Canceled" if User clicks Cancel on Dialog Box
'Thanks to: http://www.vbaexpress.com/forum/showthread.php?48553-How-to-List-Contents-Of-A-Folder-In-A-ComboBox
Dim FilePicker As Office.FileDialog
Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)
With FilePicker
.AllowMultiSelect = False
.Title = "Please select The file To Insert Into the Document"
.InitialFileName = strStartFldr
.Filters.Clear
'.Filters.Add "Word Document", "*.Doc"
If .Show = -1 Then 'User selected a folder
GetFileToInsert = .SelectedItems(1)
Else 'User pressed Cancel
GetFileToInsert = "Canceled"
End If
End With
CleanUp:
FilePicker.Filters.Clear
Set FilePicker = Nothing
End Function
Private Sub Test_GetFileToInsert()
Dim X 'Hover on X to see results.
X = GetFileToInsert
Stop 'Press F5 or F8 to continue
End Sub
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.