Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Doc's In Listbox 1

Status
Not open for further replies.

lars7

Technical User
Aug 16, 2005
817
GB
Hi Guys,
How do I get a list of word doc's, from a folder, to show in a listbox in my form.

Thanks in advance.

 
Have a look to the Dir function.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PH,

Thanks for replying,

I don't know if this is the way to do it but I tried the following code but somethings wrong with it.


Private Sub Form_Load()
Dim strLstFiles As String

strLstFiles = Dir [(C:\Marge\Docs\Enq[,0])]

Me.LstFiles.RowSource = strLstFiles

End Sub


 
You obviously get a compile error with this code.
Put the cursor inside the Dir word in your code and press the F1 key.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi Again,
I found this example but it is saying it can't find my folder, although it is there, and again the listbox isn't getting the list.


Private Sub Form_Load()
Dim MyFile, MyPath, MyName

' Return first *.TXT file with normal attribute.
MyFile = Dir("*.TXT", vbNormal)

' Display the names in C:\ that represent directories.
MyPath = "C:\Marge\Docs\Enq" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' Display entry only if it
End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop

End Sub

Sorry Ph, this is my first time to use word in my own database as you can probably tell. :-D
 
Hi Again,

I added the backward slash to MyPath and I now get the first file name showing "Doctor.doc" but only when debuggin, also Myfile shows as ""

Dim MyFile, MyPath, MyName

' Return first *.TXT file with normal attribute.
MyFile = Dir("*.Text", vbNormal)

'Display the names in C:\ that represent directories.
MyPath = "C:\marge\docs\enq\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' Display entry only if it
End If 'it represents a directory.
End If
MyName = Dir 'Get next entry.
Loop

Me.LstFiles.RowSource = MyName

End Sub

 
Here is one easy
Code:
Private Sub Form_Load()
    Dim sPath As String
    Dim sFile As String
    sPath = "C:\Zameer\"
    sFile = Dir(sPath, vbDirectory)
    Me.FilesListBox.RowSourceType = "Value List"
    Do While sFile <> ""
        If Right(sFile, 4) = ".doc" Then
            Me.FilesListBox.AddItem Item:=sFile
        End If
        sFile = Dir
    Loop
End Sub

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Hi Zameer,

This works great but can I ask if there is a way for the ".doc" not to be displayed in the ListBox.

Thanks
 
Code:
'..................

If Right(sFile, 4) = ".doc" Then
     Me.FilesListBox.AddItem Item:=[b]Left(sFile, Len(sFile) - 4)[/b]
End If
'....................

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top