i want the user to be able to open a document like word or excel by selecting
it from a combo drop list. how can i fill the drop list with the files of a
particular folder?
Copy and Paste the below code into a Module.
Public Sub GetlistFiles()
' fill a List Box with the names of files found in folder
Dim MyName As String
Dim FS As Object
Dim I As Integer
Dim strList As String
On Error GoTo Err_Handler
Set FS = Application.FileSearch
With FS
.Lookin = "C:\PathToYourFolderName\"
If .Execute(SortOrder:=1) > 0 Then
For I = 1 To .foundFiles.Count
MyName = Mid(.foundFiles(I), InStrRev(.foundFiles(I), "\") + 1)
strList = strList & MyName & ","
Next I
Else
MsgBox "There were no files found."
End If
End With
strList = Left(strList, Len(strList) - 1)
Forms!TheFormName!lstExternalFiles.RowSource = strList
Set FS = Nothing
Exit_This_Sub:
Exit Sub
Err_Handler:
MsgBox "Error #: " & Err.Number & " " & Err.Description
Resume Exit_This_Sub
End Sub
****************
Add a List Box (or a Combo Box) to your form.
Set it's RowSourceType property to Value List.
Leave it's RowSource property blank.
Name this control "lstExternalFiles".
Code the Form's Open Load event:
GetlistFiles
When the form Loads it will read the names of all files in the named
folder and fill the rowsource of the list or combo box.
To then open the selected file, either code the List box Double-click
event or the Combo Box AfterUpdate event:
Application.FollowHyperlink Me.lstExternalFiles
Change TheFormName and lstExternalFiles to whatever the actual name of
your form and list box/combo box is.