Consulting

Results 1 to 4 of 4

Thread: How to List Contents Of A Folder In A ComboBox

  1. #1

    Thumbs up How to 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 run the contents of that file (Word Document) with Microsoft Word

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

    Many Thanks

    IRduranni

  2. #2
    VBAX Contributor
    Joined
    Oct 2011
    Location
    Concord, California
    Posts
    101
    Location
    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

  3. #3
    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
    Last edited by SamT; 01-01-2014 at 10:37 AM. Reason: Removed Color Tags

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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