fill combobox with contents directory 
Author Message
 fill combobox with contents directory

Hello,
how can i fill a combobox with the contents of a specified directory,

and when i choose a file, how can i get the name of that file, without the
extension, into a variable,

thanks, schorrie



Sun, 28 Aug 2005 15:07:21 GMT  
 fill combobox with contents directory
Schorrie

Try something like the following:

Private Sub UserForm_Initialize()
    Dim strFile As String

    With ComboBox1
        strFile = Dir$("F:\My Templates\Base Folder\*.dot")
        Do Until strFile = vbNullString

            ' Add the file name without the file type to the combo box
            .AddItem Left$(strFile, Len(strFile) - 4)

            ' Get next file
            strFile = Dir$
        Loop
    End With
End Sub

Private Sub ComboBox1_Change()
    Dim strMyFile As String

    ' Put the selected combox item into strMyFile
    strMyFile = ComboBox1.List(ComboBox1.ListIndex)
End Sub

I put the code to get the selected combobox item into the comboboxes change
event handler. But it will work just as well in the OK buutons click event
or whatever.

I hope the above helps + Cheers - Peter


Quote:
> Hello,
> how can i fill a combobox with the contents of a specified directory,

> and when i choose a file, how can i get the name of that file, without
the
> extension, into a variable,

> thanks, schorrie



Sun, 28 Aug 2005 16:07:24 GMT  
 fill combobox with contents directory
Here's another example

Private Sub UserForm_Click()
'Reference to: Microsoft Sripting Runtime
Dim fso As New FileSystemObject
Dim oFiles As Files
Dim x As Integer

    'get names of files (+ extension) in combo
    Set oFiles = fso.GetFolder("c:\temp").Files
    For Each f In oFiles
        Me.ComboBox1.AddItem f.Name
    Next
    Set oFiles = Nothing
    Set fso = Nothing

 'get filename (- extension) in variable & display
 'item 10 from combobox (filenames + extension)
    Dim s As String
    Dim NameEx As String
    s = Me.ComboBox1.List(9)
    NameEx = GetFileNameEx(s$)

    MsgBox NameEx

End Sub
'*************
Function GetFileNameEx(ByVal MyFileName As String) _
As String
'function to return filename without extension
'----------------
    If InStr(MyFileName, ".") Then
    GetFileNameEx = Left$(MyFileName, InStrRev _
(MyFileName, ".") - 1)
    Else
    GetFileNameEx = MyFileName
    End If
End Function

Krgrds,
Perry

Quote:
>-----Original Message-----
>Hello,
>how can i fill a combobox with the contents of a

specified directory,
Quote:

>and when i choose a file, how can i get the name of that
file, without the
>extension, into a variable,

>thanks, schorrie

>.



Sun, 28 Aug 2005 21:16:50 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Filling ComboBox with contents of Directory

2. Filling a Secondary Combobox based on information from a primary combobox

3. Combobox and matching text contents to the combobox list

4. Fill Body of Email with contents of a Word file

5. HELP-- filling an array with the contents of Excel-file performs too too too slow-- HELP

6. Fill a listbox with the contents of a file really fast

7. Programatically fill a combobox A2K ??

8. Using the pipe symbol to fill a listbox or combobox

9. ComboBox Custom Fill Functions

10. Reference ComboBox item to fill ListBox

11. Fill a combobox

12. How to clear a filled combobox

 

 
Powered by phpBB® Forum Software