Printing worskeets in several workbooks

L

lwidjaya

I have about 20 folders with similar names such as ABC00, ABC01, ABC02,
ABC04, and so on. Most of them are consinuous. Each folder consists of an
excel file with the same name as the folder: ABC00.xls, ABC01.xls, and so on.
I need to print out the first worksheet of each file. Is there a way to do
that using macro?
Thanks in advance,
Lisa
 
D

Dave Peterson

Option Explicit
Sub testme01()
Dim iCtr As Long
Dim TestStr As String
Dim myBaseFolder As String
Dim myPfx As String
Dim myFileName As String
Dim TempWkbk As Workbook

myBaseFolder = "C:\my documents\excel"
If Right(myBaseFolder, 1) <> "\" Then
myBaseFolder = myBaseFolder & "\"
End If

myPfx = "ABC"

For iCtr = 1 To 20 'twenty folders???
'c:\my documents\excel\abc##\abc##.xls
myFileName = myBaseFolder _
& myPfx & Format(iCtr, "00") & "\" _
& myPfx & Format(iCtr, "00") & ".xls"

Set TempWkbk = Nothing
On Error Resume Next
Set TempWkbk = Workbooks.Open(Filename:=myFileName, ReadOnly:=True)
On Error GoTo 0
If TempWkbk Is Nothing Then
'warning message???
MsgBox myFileName & " wasn't opened!"
Else
On Error Resume Next
TempWkbk.Worksheets(1).PrintOut
If Err.Number <> 0 Then
MsgBox "First sheet not printed in: " & myFileName
Err.Clear
End If
On Error GoTo 0
End If
Next iCtr

End Sub

(Untested, but it did compile.)

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top