PDA

View Full Version : Solved: List Contents Of A Folder In A ComboBox



petedw
06-07-2005, 03:07 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 insert the contents of that file (Word Document) within Word at a Bookmark.

Can someone please help me with this: pray2: , i would be very grateful :yes.

Many Thanks

Pete

Killian
06-07-2005, 04:01 AM
If you have Word 2k+ (I think, or at least XP), you could just use
Dialogs(wdDialogInsertFile).Show

But to answer your question...
Option Explicit

'MAKE SURE YOU CAHNGE THIS CONSTANT TO A VALID PATH!!!
Const strStartFldr As String = "C:\Documents and Settings\Killian\"
Dim fso, froot, fldr, f

Private Sub UserForm_Initialize()

Set fso = CreateObject("Scripting.FileSystemObject")
Set froot = fso.GetFolder(strStartFldr)
For Each fldr In froot.SubFolders
ComboBox1.AddItem fldr.Name
Next
ComboBox1.ListIndex = 0

End Sub

Private Sub ComboBox1_Change()

ComboBox2.Clear
Set froot = fso.GetFolder(strStartFldr & ComboBox1.Text)
For Each fldr In froot.SubFolders
ComboBox2.AddItem fldr.Name
Next
If ComboBox2.ListCount > 0 Then ComboBox2.ListIndex = 0

End Sub

Private Sub ComboBox2_Change()

ComboBox3.Clear
Set froot = fso.GetFolder(strStartFldr & ComboBox1.Text & "\" & ComboBox2.Text)
For Each f In froot.Files
If UCase(Right(f.Name, 3)) = "DOC" Then
ComboBox3.AddItem f.Name
End If
Next
If ComboBox3.ListCount > 0 Then ComboBox3.ListIndex = 0

End Sub

Private Sub CommandButton1_Click()

With ActiveDocument.Bookmarks("bkTest")
.Range.InsertFile (strStartFldr & ComboBox1.Text & "\" & ComboBox2.Text & "\" & ComboBox3.Text)
End With
Unload Me

End Sub

Private Sub UserForm_Terminate()

Set f = Nothing
Set fldr = Nothing
Set froot = Nothing
Set fso = Nothing

End Sub

petedw
06-07-2005, 04:21 AM
:clap: Killian you are the man! :bow:

Thank you!!

MOS MASTER
06-07-2005, 09:47 AM
Hi K, :yes

Me like it....Simple and effective! :hi: (I didn't look buth if we don't have this one yet you should submit it as a KB Entry....Looks great)