This is the code that I am using to print ll files in a folder:
Sub PrintFilesFromListbox()
Dim path As String
Dim FileName As String
Dim Wkb As Workbook
Dim WS As Worksheet
Application.EnableEvents = False
Application.ScreenUpdating = False
PATH = "D:\Form16_AY_2008 - 2009\"
FileName = Dir(PATH & "\*.xls", vbNormal)
Do Until FileName = ""
Set Wkb = Workbooks.Open(FileName:=PATH & "\" & FileName)
Application.ActivePrinter = "CutePDF Writer on CPW2:"
Wkb.PrintOut Copies:=1, Collate:=True
Wkb.Close False
FileName = Dir()
Loop
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
This is working well.
This is the code that I am using to add all the files name in the folder
into a listbox:
Sub newlistbox()
Dim i As Integer, fNm As String, fLg As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
On Error Resume Next
With Application.FileSearch
.NewSearch
'Change path To suit
.FileType = msoFileTypeExcelWorkbooks
.LookIn = "D:\Form16_AY_2008 - 2009\"
If .Execute > 0 Then 'Workbooks In folder
For i = 1 To .FoundFiles.Count ' Loop through all
fLg = InStrRev(.FoundFiles(i), "\")
fNm = Right(.FoundFiles(i), Len(.FoundFiles(i)) - fLg)
UserForm1.ListBox1.AddItem Replace(fNm, ".xls", "")
Next i
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
'MsgBox "Form Loaded"
End Sub
Now I want to Print those files which are selected in the Listbox.
If all the files are selected print all the files.
If some files are selected, then print those selected files only.
If nothing is selected then nothing to be printed.