Home » excel » vba – Excel Macro: iterate through workbooks and print individual sheets from each

vba – Excel Macro: iterate through workbooks and print individual sheets from each

Posted by: admin May 14, 2020 Leave a comment

Questions:

The steps I need to carry out are repetitive, but I’m not sure how to iterate through each workbook and then each sheet.

My task is to:

  1. Look in a folder: (Source folder)
  2. Iterate through each workbook in that folder (Source file)
  3. Print each of the 4 worksheets (sheet name) in each workbook to a PostScript Printer (Printer Name/Path).
  4. Name the printed to file PS file = Source file+sheet name
  5. Final PS output files placed in final folder (destination folder)
  6. Original workbook closed and not saved.

I searched for iteration VBA/Macro and have seen some ideas, but I’m unsure how the code looks when it’s working through workbooks and worksheets.

Also, the PS printer is done through Printing To File. Does this cause problems.

UPDATED WITH CODE I’VE TRIED SO FAR:

Sub Make_PS_Files()

Dim path2 As String, path3 As String

path2 = "Drive:\Source folder\"
path3 = " Drive:\Destination folder\"

Workbooks.Open Filename:=path2 + "File_Name.XLS"
Sheets("Specific_Sheet_Name1").Activate

  Application.ActivePrinter = "\PRINTER NAME\LOCATION:"

  ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
   :=True, Prtofilename:=True
   ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name1.ps"


Sheets("Specific_Sheet_Name2").Activate

  Application.ActivePrinter = "\VS PRINTER NAME\LOCATION:"

  ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
   :=True, Prtofilename:=True
   ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name2.ps"


Sheets("Specific_Sheet_Name3").Activate

  Application.ActivePrinter = "\ PRINTER NAME\LOCATION:"

  ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
   :=True, Prtofilename:=True
   ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name3.ps"
ActiveWorkbook.Close


Sheets("Specific_Sheet_Name4").Activate

  Application.ActivePrinter = "\ PRINTER NAME\LOCATION:"

  ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
   :=True, Prtofilename:=True
   ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name4.ps"
ActiveWorkbook.Close

End Sub

Apologies for not posting this last night when I posted. It seems very long winded for what it is doing. I thought it could be polished a bit more so it is more universal and can be pointed at any workbook and any number of sheets.

Not all the sheets have the Specific_Sheet_Name, so I would like to iterate through without reference to the name.

How to&Answers:

You can iterate through the workbooks in a folder using the FileSystemObject by using the Folder and File objects. (Remember to add a reference to the Microsoft Scripting Runtime)

Iterating through worksheets is as easy as a For Each over the Workbook.Sheets collection.

Finally, you can print the worksheet using the PrintOut method on each worksheet to print them. Just make sure to set the PrintToFile parameter to true.

If you need more direction than that, I suggest following @JMax’s advice and post some of what you’ve tried already.

UPDATE

To iterate through workbooks using FileSystemObject:

Sub Make_PS_Files()
  Dim fso As New Scripting.FileSystemObject
  Dim source As Scripting.Folder
  Dim wbFile As Scripting.File
  Dim book As Excel.Workbook

  Set source = fso.GetFolder("Drive:\Source folder\")
  For Each wbFile In source.Files
    If fso.GetExtensionName(wbFile.Name) = "xls" Then
      Set book = Workbooks.Open(wbFile.Path)
      ' Print out the workbook
    End If
  Next 
End Sub

And to iterate through the worksheets in a workbook:

Dim sheet As Excel.Worksheet

For Each sheet in book.Sheets
  sheet.PrintOut Copies:=1, ActivePrinter:="\Printer:", PrintToFile:=True, _
                 PrToFileName:=fso.BuildPath(destination, sheet.Name & ".ps")
Next

Keep in mind that this has no error handling, but I hope it helps.