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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing Excel sheets rather than the Whole workbook.

Status
Not open for further replies.

balistikb

Technical User
Nov 12, 2002
177
US
Can someone tell me how do I write code to print a specific spreadsheet rather than the whole workbook in excel from VB?
Here is what I got so far:
Dim EndWait 'As Double
Dim objApp 'As Excel.Application
Dim DailyFile 'As Workbook
Dim PrinterPath, WshNetwork

Set WshNetwork = WScript.CreateObject("WScript.Network")
PrinterPath = "\\REGIONAL\NDPS-P36V-FIN-T620.FIN.MAB.MHS"
'WshNetwork.AddWindowsPrinterConnection(PrinterPath)
WshNetwork.SetDefaultPrinter PrinterPath

'WScript.Echo " New default printer is " & PrinterPath
'If MsgBox ("This program will print a spreadsheet by automating Microsoft Excel. OK to continue?", vbOkCancel) = vbCancel Then Wscript.Quit

Set objApp = CreateObject("Excel.Application")

'''''''''' Remove the below line if you don't want to watch Excel
objApp.Visible = True

'''''''''' Replace the web page with the one you want printed.
'Set doc = wrd.Documents.Open("Set DailyFile = objApp.Workbooks.Open("I:\Daily Statistics Report\Daily Statistics.xls")

'''''''''' This is a messy kluge to give the page time to load
'''''''''' If you try to print before the page is loaded, Word crashes!
EndWait = Now + (20 / 86400) ' 20 second delay to let page load
Do Until Now > EndWait
Loop

'''''''''' Un-comment the below line if you want to use a printer
'''''''''' other than your default. If you specify a printer,
'''''''''' it must be one that is already installed on your system.
'objApp.ActivePrinter = ".NDPS-IT-S2455.IT.MSS.MHS" ---> THIS DID NOT WORK
'objApp.ActivePrinter = "Lexmark Optra S 2455" ---> THIS DID NOT WORK
'objApp.ActivePrinter = "\\Regional\.NDPS-IT-S2455.IT.MSS.MHS" ---> THIS DID NOT WORK
DailyFile.printout

PrinterPath = "\\Regional\.NDPS-IT-T612.it.mss.mhs"
'WshNetwork.AddWindowsPrinterConnection(PrinterPath)
WshNetwork.SetDefaultPrinter PrinterPath
objApp.quit
'WScript.Echo " Printing has started. New default printer is " & PrinterPath
Set DailyFile = Nothing
Set objApp = Nothing
Set WshNetwork = Nothing
 
Try this... It works with VBA...

'Open workbook
Workbooks.Open FileName:="C:\Example.xls", _
UpdateLinks:=1

'Print specific sheet in workbook
Sheets("Sheet1").Select
ActiveSheet.PrintOut
ActiveWorkbook.Close
 
There is an excellent download from
In the VBA categrory there's a file called sel_sheets_2_print.zip

This example shows you how to select one or more of a Workbook's sheets for print.

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top