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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture all .doc files from directory 2

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
This might be the wrong forum although the acticex forum looks quiet.

I have an Access form on which there is a common dialogue control. I am trying to fill a list, eventually a table, with all the paths/documents names (.doc) from a selected directory. At the moment I can get one at a time, but its taking a long time to fill a list. Can the code be modified in any way, or anyone got other ideas. Many thanks

Private Sub Command5_Click()
Dim strFilter As String
Dim strInputFileName As String

'strFilter = ahtAddFilterItem(strFilter, "JPEG Files (*.jpg, *.jpeg)", "*.jpg;*.jpeg")
strFilter = ahtAddFilterItem(strFilter, "doc Files (*.doc)", "*.doc")
' strFilter = ahtAddFilterItem(strFilter, "all Files (*.*)", "*.*")

strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, _
OpenFile:=True, _
DialogTitle:="Choose a Transcription file...", _
Flags:=ahtOFN_HIDEREADONLY)

If Len(strInputFileName) > 0 Then
'MsgBox "You have just selected this Path: " & strInputFileName
Me.STOREDPATH.AddItem strInputFileName
Me.Command5.SetFocus
Command5_Click
Else
'No file chosen, or user canceled
End If

End Sub
 
Here is one way:

Code:
Dim fs As FileSystemObject
Dim fol As Folder
Dim fil As Object 'File

Set fs = CreateObject("Scripting.FileSystemObject")
Set fol = fs.getFolder("C:\Docs")

For Each fil In fol.files
    If fil.Type = "Microsoft Word Document" Then
        Debug.Print fil.Name
    End If
Next

There are others, including Dir.
 
Thanks Remou. I get an error on the filesystemobect, type not defined.?? Regards
 
Oops. It is not essential to define it, you can say:

[tt]Dim fs As Object 'FileSystemObject
Dim fol As Object 'Folder
Dim fil As Object 'File[/tt]

If you wish to add a reference, it is:
Windows Script Host Object Model
 
Many thanks. I put in a referece and it worked. I tried adding the files in a listbox but it did not like it?

For Each fil In fol.files
If fil.Type = "Microsoft Word Document" Then
Me.STOREDDOCS.AddItem fil.Nam

Debug.Print fil.Name
End If
Next

Can they be extracted into a list? Regards
 
Does your version of Access allow AddItem for listboxes? If so, you need an 'e'

Me.STOREDDOCS.AddItem fil.Nam[red]e[/red]

You can easily create a list:

lst=lst & "; " & fil.Name

Don't forget to strip the initial characters from the list.
 
Many thanks Remou, the extra letter E made it work. Excellent, thanks for your help.
 
Bit more complication

I started to use the commondialog to get a document from a selected directory. That returned the drive/directory/filename.doc. Now only the directory path is required to trigger the further bit.

Then used the code below which is excellent, but I am trying to marry the two together in that a directory/path can be selected, but then each sub directory be looked into and any word documents be put in a list, to include all directory path through to document name.

Can it be done?


Dim fs As FileSystemObject
Dim fol As Folder
Dim fil As Object 'File

Set fs = CreateObject("Scripting.FileSystemObject")
Set fol = fs.getFolder("C:\Docs")

For Each fil In fol.files
If fil.Type = "Microsoft Word Document" Then
Debug.Print fil.Name
End If
Next
 
Thankyou very much Remou, I owe you a star for that. I can use that okay. Next problem is I want to select a directory and then be able to fill a list of all the subdirectory names, ie one level down. Then I'm home an dry for a bit. Regards
 
The Lebans link in the link above should be what you need. Alternatively, you can use the code above to return subfolders:

[tt]For Each sfol In fol.SubFolders
Debug.Print sfol.Name
Next[/tt]
 
Thanks Remou. Where do I put your last post code. I tried it within the lebans code, set sfol as folder and fol as folders but it wanted an object. I tried it in a command button but did not work. I am trying to get a list that fills like

c:\one\cards\one.doc
c:\one\cards\two.doc
c:\one\wines\one.doc

etc, where the user hit on C:\one

Regards

 
The snippet fits into the previous snippet. How about you put all the pieces together and post back your version with notes on where you are having problems?
 
Thankyou Remou, I got there. Sorry I misunderstood the last post.

Private Sub Command10_Click()
Dim fs As FileSystemObject
Dim fol As Folder
Dim fil As Object 'File
Dim sfol As Folder

Set fs = CreateObject("Scripting.FileSystemObject")
Set fol = fs.getFolder("K:\")

For Each sfol In fol.SubFolders
Me.LSubfolders.AddItem sfol.Name
Next

For Each fil In fol.files
If fil.Type = "Microsoft Word Document" Then
Me.STOREDPATH.AddItem fil.Name
End If
Next


End Sub


I owe you a few more stars, Regards and thanks
 
Sorry, back again. Is there any way of prestarting the drive to search directory on?

strFolderName = BrowseFolder("What Folder you want to select?")

Probably not, but thought I would ask.
 
Thanks again. Can you tell me where I can get some glasses from!!

Thats it, thread closed. I hope a lot of other people get something from this thread, and come in to give you more stars. Thanks again
 
Thought I had it all. How do I modify this code

For Each sfol In fol.SubFolders
Me.D2.AddItem sfol.Name
Next

For Each fil In fol.files
If fil.Type = "Microsoft Word Document" Then
Me.D1.AddItem fil.Name
End If
Next

Where for each subfolder, word documents are found, and if possible added to a list with subfolder Name & Document name.

I tried to put a loop within a loop and got nowhere

Thanks
 
Are you sure that this is what you want? That is, to select a folder and to list the Word Documents one level down from that, but not two levels and not in the folder itself?

Code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set fol = fs.getFolder(FolderName)
For Each sfol In fol.SubFolders
    Me.D2.AddItem sfol.Name
    For Each fil In sfol.files
        If fil.Type = "Microsoft Word Document" Then
            Me.D1.AddItem fil.Name
        End If
    Next
Next

However, would it not be better to build the document list when the subfolder is selected from D2? That is, the document list will show documents in the selected folder.
 
Hi Remou,

At the moment I am using the selected D2 to get a list of documents,as in your last line

You could be correct in thinking my levels are wrong.

I start with a directory of:

DDS = "K:\first\second\third"

Within the directory of Third there are many other directories each containing Word Documents.

I am wanting to loop through all the subdiectories retrieving all the document names so I end up with a list filled with

"K:\First\Second\Third\cricket\something.doc"
"K:\First\Second\Third\cricket\another.doc"
"K:\First\Second\Third\football\another.doc"

etc

Many thanks



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top