Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help On Printing Code 1

Status
Not open for further replies.

wom2007

Technical User
May 1, 2007
11
US
Hi,

I am trying to create a code that will allow me print all the sheets in several workbooks that is located in a similar directory folder. I started out with the following code, but I cannot get it to work.

Sub TempPrintMultWkbk()

Dim PathNm As String
PathNm = ActiveWorkbook.Path
Dim FSO, Fldr, Fle, Fls
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Fldr = FSO.GetFolder(PathNm)
Set Fls = Fldr.Files
For Each Fle In Fls
If Right(Fle, 4) = ".xls" Then
Workbooks.Open Filename:=Fle
Workbooks(Fle).Activate
For Each sht In Sheets
sht.PrintOut
Next sht
Workbooks(Fle).Close savechanges:=False
End If
Next
End Sub


I keep runing into errors when I reach the section 'Workbooks(Fle).Activate'. I also have issues when the workbooks have links in them. Can someone provide a solution or a new code for me to use. Thanks for your help.
 
Code:
Sub TempPrintMultWkbk()
   Dim fso As Object, folder_name As Object, file_name As Object, curr_sheet As Object
   Dim path_name As String
   
   path_name = ActiveWorkbook.Path
   Application.DisplayAlerts = False
   
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set folder_name = fso.GetFolder(path_name)

   For Each file_name In folder_name.files
      If Right(file_name, 4) = ".xls" Then
         Workbooks.Open file_name, UpdateLinks:=1

         For Each curr_sheet In ActiveWorkbook.Sheets
            curr_sheet.Activate
            curr_sheet.PrintOut
            DoEvents
         Next
         
         ActiveWorkbook.Close
      End If
   Next
End Sub
 

WinblowsME,

You're on the right track BUT...

as you have defined
[tt]
folder_name As Object, file_name As Object
[/tt]

these are OBJECTS.

1) Better to name like this
[tt]
oFolder As Object, oFile As Object
[/tt]

Name is a PROPERTY of the File Object as
Code:
   For Each oFile In oFolder.Files
      If Right(oFile.Name, 4) = ".xls" Then


Skip,

[glasses] [red][/red]
[tongue]
 
Thank you WinblowsME. Your code worked like magic. It was just what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top