Printing sheet array in workbook, looping in file

Mathexcel

New Member
Joined
Jun 22, 2017
Messages
36
Hi,
I need to do a print specific sheets in 30 workbooks. The sheet names remain the same. I save my files in a folder and want to execute my printing from a document with a printing macro that would allow me to select the folder (or documents) and print the sheets.

I have tried the following code but without success. The code jumps to then MsgBox without printing anything. Could anyone advise on what I am doing wrong?
Code:
Sub LoopAllExcelFilesInFolderForPrinting()


Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog


'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)


    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & ""
    End With


'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings


'Target File Extension (must include wildcard "*")
  myExtension = "*.xlsm*"


'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)


'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
      Set wb = Workbooks.Open(Filename:=myPath & myFile)
      
      DoEvents
           
        wb.Worksheets(Array("Comp", "Response", "Day of Week", "Segmentation at Glance", "Segmentation Ranking", "Segmentation DOW YTD", "Segmentation Response", "Summary")).PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
        
        wb.Close
      
      DoEvents


    'Get next file name
      myFile = Dir
  Loop


'Message Box when tasks are completed
  MsgBox "Printing done"


End Sub


Thank you in advance.
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I've tweaked your macro.
Code:
Sub LoopAllExcelFilesInFolderForPrinting()

    Dim wb As Workbook
    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String
    Dim FldrPicker As FileDialog
    
    'Retrieve Target Folder Path From User
    Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
    With FldrPicker
        .Title = "Select A Target Folder"
        .AllowMultiSelect = False
        If .Show Then
            myPath = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    
    myPath = myPath & "\"
    
    'Target File Extension (must include wildcard "*")
    myExtension = "*.xlsm*"
    
    'Target Path with Ending Extention
    myFile = Dir(myPath & myExtension)
    
    'Loop through each Excel file in folder
    Do While myFile <> ""
        'Set variable equal to opened workbook
        Set wb = Workbooks.Open(Filename:=myPath & myFile)
        
        DoEvents
        
        wb.Worksheets(Array("Comp", "Response", "Day of Week", "Segmentation at Glance", "Segmentation Ranking", "Segmentation DOW YTD", "Segmentation Response", "Summary")).PrintOut Copies:=1, Collate:=True, _
            IgnorePrintAreas:=False
        
        wb.Close
        
        DoEvents
        
        'Get next file name
        myFile = Dir
    Loop
    
    'Message Box when tasks are completed
    MsgBox "Printing done"
       
End Sub
 
Upvote 0
Hum the macro sends me back directly to my MsgBox without doing anything.

It seems like the documents are not even opened...
 
Upvote 0
This line:
Code:
  myExtension = "*.xlsm*"
means the code is looking for macro-enabled workbooks (.xlsm files). Are the workbooks .xlsm files?

If not, change that line to:
Code:
  myExtension = "*.xls*"
and then it should look for and open any type of Excel workbook.
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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
Back
Top