Loop folder and print macro

Gon

New Member
Joined
Nov 19, 2012
Messages
5
Hi all Excel Experts,

I have a vba code to loop folder and print macro. I tried running the code and noticed that for excel files with extention xls & xlsm, it run promptly but for excel files with xlsx, it takes about 5 min to execute the command. Any ideas why? Thank you.
Code:
Sub PrintTest()
Dim wbSrc As Workbook
Dim wsSrc As Worksheet
Dim MyPath As String
Dim strFilename As String
    
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    MyPath = "D:\xxx\Excel\" 
    strFilename = Dir(MyPath & "\*.xls")
    
    If Len(strFilename) = 0 Then Exit Sub
    
    Do While strFilename <> ""
   
        
            Set wbSrc = Workbooks.Open(Filename:=MyPath & "\" & strFilename)
            
            Set wsSrc = wbSrc.Worksheets("xxxxx")
            wsSrc.PrintOut
            
            Set wsSrc = wbSrc.Worksheets("yyyyy")
            wsSrc.PrintOut
            
            
            wbSrc.Close savechanges:=False
        
        strFilename = Dir()
        
    Loop
    
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Are the xlsx files bigger than the other types?

try saving an XLS and an XLSX and see if it is the type of file that takes longer ( cos normally its the overall size that matters)


I tidied this up a bit

Code:
Sub PrintTest()
Dim wbSrc As Workbook
Dim wsSrc As Worksheet
Dim MyPath As String
Dim strFilename As String
    
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    MyPath = "D:\xxx\Excel\"
    strFilename = Dir(MyPath & "*.xls*")
    
    If Len(strFilename) = 0 Then Exit Sub
    
    Do While strFilename <> ""
   
            DoEvents
            Set wbSrc = Workbooks.Open(Filename:=MyPath & strFilename)
            
            Set wsSrc = wbSrc.Worksheets("xxxxx")
            wsSrc.PrintOut
            
            Set wsSrc = wbSrc.Worksheets("yyyyy")
            wsSrc.PrintOut
            
            
            wbSrc.Close savechanges:=False
            Set wsSrc = Nothing
            Set wbSrc = Nothing
            DoEvents
            
        strFilename = Dir()
        
    Loop
    
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,210
Members
448,874
Latest member
b1step2far

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