Printing with VBA's help...

samilynn

Board Regular
Joined
Jun 24, 2003
Messages
141
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
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

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
 

Forum statistics

Threads
1,141,144
Messages
5,704,544
Members
421,354
Latest member
Canary99

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top