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

Printing folder contents

Status
Not open for further replies.
Jan 26, 2007
3
GB
Hi hoping that someone will be able to help me with this problem that has been taxing me for months.

I have the following code which allows the user to view folders in a list box.

Dim objFSO As Object, TargetFolder As Object, FC As Object

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set TargetFolder = objFSO.GetFolder("C:\Documents and Settings\desktop\H&S")
Set FC = TargetFolder.SubFolders
HowMany = FC.Count


For Each File In FC
ListBox1.AddItem (File.Name)
Debug.Print File.Name
Next File



I want to be able to have a command button which will allow the user to select folder in the above list box and print its entire contents (word docs only) including any contents of subfolders.
 
What have you tried so far ?
Where are you stuck in your code: retrieve the .doc files in the selected folder and/or print them ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
your use of File is pretty misleading considering you have a collection of Folders
 
Have tried the following code with no success as Im unsure what goes where, do I just cut a paste this to a command button ?

Dim dlgSelect As FileDialog, selectedFolder As String
Set dlgSelect = Application. FileDialog(msoFileDialogFol derPicker)
dlgSelect.Show
selectedFolder = dlgSelect.InitialFileName

Dim fso As New FileSystemObject
printDocumentsInFolder fso.GetFolder(selectedFolder)

End Sub

Private Sub printDocumentsInFol der(folderToPrin t As Folder)
Dim fileInFolder As File
For Each fileInFolder In folderToPrint. Files
If fileInFolder. Type = "Microsoft Word Document" Then
Dim docToPrint As Word.Document
Set docToPrint =
Application. Documents.Open(FileName: =fileInFolder. Path, ReadOnly:=True)
docToPrint.PrintOut
docToPrint.Close SaveChanges: =False
End If
Next

Dim subFolder As Folder
For Each subFolder In folderToPrint. SubFolders
printDocumentsInFol der subFolder
Next

End Sub


 
Try this;

Sub Macro1()

Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)

If Not dlgOpen.Show Then Exit Sub

With Application.FileSearch
.NewSearch
.LookIn = dlgOpen.SelectedItems(1)
.SearchSubFolders = True
.FileType = msoFileTypeWordDocuments
If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
With Documents.Open(FileName:=.FoundFiles(i), ReadOnly:=True)
.PrintOut
.Close
End With
Next i
Else
MsgBox "There were no files found."
End If
End With

Set dlgOpen = Nothing

End Sub

regards Hugh,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top