macro to print all office documents in a directory?

Kyle775

Board Regular
Joined
Feb 16, 2005
Messages
230
someone provided me code earlier this week on how to print all the xls sheets in a directory... is it possible to alter that code to include word documents?
lemme find the code...

Code:
Sub PrintAllWS()
Dim wb As Workbook
Dim ws As Worksheet
Dim MyFolder
Dim I As Long

    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    MyFolder = GetFolder
    If MyFolder = vbNullString Then
        MsgBox "No folder selected. Please select a folder to print.", vbCritical
        Exit Sub
    End If
    
    With Application.FileSearch
        .NewSearch
        .LookIn = MyFolder
        .FileType = msoFileTypeExcelWorkbooks
        .Execute

        For I = 1 To .FoundFiles.Count
            Set wb = Workbooks.Open(.FoundFiles(I))
            For Each ws In wb.Worksheets
                 ws.PrintOut
            Next ws
            wb.Close
        Next I
    End With
    
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

Private Function GetFolder() As String
'common UDF for getting a folder name
    Dim ff As Object
    Set ff = CreateObject("Shell.Application"). _
             BrowseForFolder(0, "Please select a folder", 0, "c:\\")
    If Not ff Is Nothing Then
        GetFolder = ff.Items.Item.Path
    Else
        GetFolder = vbNullString
    End If
End Function

i would like to include subdirectories if that is possible...
choose a single "root" and print all worksheets from all work books and all word documents in all the sub directories in the order they appear in windows
 
well i want the word documents to print before the excel documents that they appear before in windows explorer.
for example i have customerA.doc and customberA.xls where xls has 6 sheets. i want the doc file first and then workbook with all 6 sheets printed. there are too many word documents to use the explorer print option, it crashes my PC, opening and closing so many documents at a time and it would not allow me to have the word documents printing before each workbook.
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
So basically you're doing this so you don't need to sort the documents by hand?:)
 
Upvote 0
Yep.:)

But is it the correct tool for the job?

Early I was thinking of using some other code that will print any document no matter what the type but I couldn't find it.

Then you wouldn't need to open Word. I think it's some kind of Shell stuff but I can't seem to find it.

I'll have another look.
 
Upvote 0
i think that would be much cleaner... as long as the formatting is maintained etc.

thank you so much...
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,812
Members
449,095
Latest member
m_smith_solihull

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