Printing with VBA's help...

samilynn

Board Regular
Joined
Jun 24, 2003
Messages
137
I want to print all workbooks in a specific folder, and I found this VBA code on a thread in this forum.
What am I doing wrong, I can't get it to work??
thank you so much

Sub PrintAllWorkbooksInFolder(TargetFolder As String, FileFilter As String)
' prints all workbooks in a folder that matches the FileFilter
' example: PrintAllWorkbooksInFolder "C:\FolderName", "*.xls"
' example: PrintAllWorkbooksInFolder "C:\FolderName", "Bud*.xls"
Dim fn As String, sht As Variant
Application.ScreenUpdating = False
If Right(TargetFolder, 1) <> Application.PathSeparator Then
TargetFolder = TargetFolder & Application.PathSeparator
End If
If FileFilter = "" Then FileFilter = "*.xls"
fn = Dir(TargetFolder & FileFilter) ' the first file name in the folder
While Len(fn) > 0
If fn <> ThisWorkbook.Name Then
Application.StatusBar = "Printing " & fn & "..."
Workbooks.Open TargetFolder & fn
ActiveWorkbook.PrintOut ' prints all sheets in the workbook

' or print each separate sheet
' For Each sht In ActiveWorkbook.Sheets
' sht.PrintOut
' Next sht

' print a specific sheet or chart
' Worksheets(1).PrintOut ' prints the first worksheet in the workbook
' Charts(2).PrintOut ' prints the second chart sheet in the workbook

' print all sheets of a specific sheet type
' For Each sht In ActiveWorkbook.Sheets
' Debug.Print ActiveWorkbook.Name & " " & _
sht.Name & " is of type " & TypeName(sht) ' can be removed...
' If TypeName(sht) = "Worksheet" Then
' sht.PrintOut ' print worksheets
' End If
' If TypeName(sht) = "Chart" Then
' sht.PrintOut ' print charts
' End If
' Next sht
ActiveWorkbook.Close False
' close the workbook without saving any changes
End If
fn = Dir ' the next file name in the folder
Wend
Application.StatusBar = False
End Sub
 

Some videos you may like

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Bruno

Active Member
Joined
Feb 17, 2002
Messages
491
You need to start this routine with some parameters :

Code:
Sub PrintMyWorkbooks()
PrintAllWorkbooksInFolder "c:\MyExcelFiles", "2006*.xls"
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,109,466
Messages
5,528,972
Members
409,848
Latest member
Blomsten
Top