getting a filelist (1 Viewer)

jesse

Registered User.
Local time
Today, 08:07
Joined
Jul 14, 2010
Messages
39
Hi,

I want to get a filelist from a specific directory (in order to import the files later on). I pulled some code of the internet and adjusted it a bit but I can't get it to work. I've pasted the code below. For some reason the part of the code inside the "With Application.FileSearch - end with" is skipped completely (if I put a msgbox within the with statements it isn't shown).

Does anyone know what I did wrong here? Any help would be much appreciated.

Kind regards,
jesse

(f_list is a dictionary object)

Sub get_filelist(src_map As String)


Dim objFSO As FileSystemObject, objFolder As Folder
Dim objFile As File, x As Long

Set objFSO = New FileSystemObject


MsgBox ("filelist functie")

f_list.RemoveAll

With Application.FileSearch
.LookIn = src_map 'look in the folder browsed to
.FileType = msoFileTypeAllFiles 'get all files
.SearchSubFolders = False
.Execute 'run the search

For x = 1 To .FoundFiles.Count 'for each file found, by the count (or index)


On Error GoTo Skip 'in the event of a permissions error

Set objFile = objFSO.GetFile(.FoundFiles(x)) 'set the object to get it's properties


f_list.Add x, objFile.Name


' Next objFile
Skip:

Next x


End With




End Sub
 

Trevor G

Registered User.
Local time
Today, 08:07
Joined
Oct 1, 2009
Messages
2,341
Which Version of MS Office are you using? If 2007 then the folder search facility has been removed and you have to use another method.

Let us know which version and code can be provided. I use a list box on a form and then show files of certain types in the list box, then if I need to open it I can add some extra code to open based on the select file and double click to open it.
 

jesse

Registered User.
Local time
Today, 08:07
Joined
Jul 14, 2010
Messages
39
Hi,

tnx for the response. I'm working in access 2010. Is this a version problem?

kind regards,
jesse
 

Trevor G

Registered User.
Local time
Today, 08:07
Joined
Oct 1, 2009
Messages
2,341
Hi,

tnx for the response. I'm working in access 2010. Is this a version problem?

kind regards,
jesse

Jesse what you can do is create a form with a listboxon it. Then behind the form events you can add the following:

The Const will go at the top of the VBA screen and change the path accordingly.

Const strdirpath = "M:\Access Files"

Private Sub Form_Load()
Dim strfile As String
ChDir strdirpath
strfile = Dir("*.*")
Do Until strfile = ""
Me.lstfiles.AddItem strfile
strfile = Dir
Loop
End Sub

The list box I added I named lstfiles as indicated above. You have to change the properties row source to Value list.


If you then want to open the files you can add the following:

Private Sub lstfiles_Click()
Dim app As Object
Select Case LCase(Right(Me.lstfiles.Value, 3))
Case "xls"
Set app = CreateObject("Excel.Application")
app.Visible = True
app.workbooks.Open strdirpath & "\" & Me.lstfiles.Value
Case "doc"
Set app = CreateObject("Word.Application")
app.Visible = True
app.Documents.Open strdirpath & "\" & Me.lstfiles.Value
Case "txt"
Set app = CreateObject("Word.Application")
app.Visible = True
app.Documents.Open
Case "htm"
Set app = CreateObject("Word.Application")
app.Visible = True
app.Documents.Open
Case Else
MsgBox "Cannot open this file type"
End Select
Set app = Nothing
End Sub
 
Last edited:

jesse

Registered User.
Local time
Today, 08:07
Joined
Jul 14, 2010
Messages
39
Well, thanks a lot to the both of you.

Trevor, your code worked like a charm.

kind regards,
Jesse
 

Users who are viewing this thread

Top Bottom