Consulting

Results 1 to 4 of 4

Thread: Solved: List Contents Of A Folder In A ComboBox

  1. #1
    VBAX Regular
    Joined
    Apr 2005
    Posts
    86
    Location

    Solved: List Contents Of A Folder In A ComboBox

    Hi,

    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 , i would be very grateful .

    Many Thanks

    Pete

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    If you have Word 2k+ (I think, or at least XP), you could just use
    [VBA]Dialogs(wdDialogInsertFile).Show[/VBA]

    But to answer your question...
    [VBA]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[/VBA]
    K :-)

  3. #3
    VBAX Regular
    Joined
    Apr 2005
    Posts
    86
    Location
    Killian you are the man!

    Thank you!!

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi K,

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

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •