Focal Point Banner
Community Center Education Summit Technical Support User Groups
Let's Get Social!

Facebook Twitter LinkedIn YouTube
Focal Point    Focal Point Forums  Hop To Forum Categories  WebFOCUS/FOCUS Forum on Focal Point     <SOLVED Macro to automatically print out an Excel
Go
New
Search
Notify
Tools
Reply
  
<SOLVED Macro to automatically print out an Excel
 Login/Join
 
Guru
posted
I realize that this discussion is somewhat beyond the realm of the Focal Point forum but since so many people do templates for EXL2K output:

Does anybody have VBA macro code that will

1. Automatically designate a print area
2. Print only populated cells
3. Return 0” margins on all sides
4. Fix the rows at the top of the page so that these rows will always appear

I’ve been researching online using Messrs. Bing and Google but felt that the Focus Nation might be a good place to search

All the best and thanks in advance for your sage advice,
Sandy Cool

This message has been edited. Last edited by: S.J. Kadish,


Sandy Kadish
Dev: 8.2.04- PostgreSQL
Test: 8.2.04 - PostgreSQL
Prod: 8.2.04 - PostgreSQL
 
Posts: 239 | Location: Orlando, FL / Baltimore, MD | Registered: May 06, 2003Reply With QuoteReport This Post
Expert
posted Hide Post
Lets hope that we are a prosperous nation.



Tony A is a big one for VB macros, perhaps he can help.


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.05OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Know The Code

 
Posts: 6068 | Location: Land of the Darug people, Terra Australis Incognita | Registered: October 31, 2006Reply With QuoteReport This Post
Master
posted Hide Post
i have not personally done this and have not tested the below to work. However I would do the following:

Place code below (or a call to the code below) in the Workbook.open event.

-Set Print Area

Sub Set_Print_Area()
Dim x As Long, lastCell As Range
x = ActiveSheet.UsedRange.Columns.Count
Set lastCell = Cells.SpecialCells(xlCellTypeLastCell)
ActiveSheet.PageSetup.PrintArea = Range(Cells(1, 1), lastCell).Address
End Sub


Source: http://www.mrexcel.com/archive/Printing/30490.html

(or look at the Excel.PageSetup.PrintArea method)


- Print the Worksheet

Excel.WorkSheet.PrintOut

(example: ActiveSheet.PrintOut)
See Help file in Excel VBA editor for usage example.

- I'd suggest against 0" margins unless you are working with a specific printer that can handle that. Otherwise, I'd set this in the FEX.

ON TABLE SET STYLE *
     UNITS=IN,
     LEFTMARGIN=0.000000,
     RIGHTMARGIN=0.000000,
     TOPMARGIN=0.000000,
     BOTTOMMARGIN=0.000000,
     SQUEEZE=ON,
$



- "Fix the rows at the top of the page so that these rows will always appear"

This is called FreezePane and you can find details here: http://www.automateexcel.com/2...el_vba_freeze_panes/

(comment: if you're automatically printing the spreadsheet isn't a freeze pane a moot point? It will have no effect on the printed page). Perhaps you want a header instead? (will print across all pages). If so, see here: http://www.cpearson.com/excel/headfoot.htm

** My assumption is that you have some experience with VBA and/or MS Office automation with VBA. If not, this would be a good place to start: http://www.functionx.com/vbaexcel/Lesson01.htm

** I hope this helps. I'm sorry I don't myself have the time to write and test the code you request, but hopefully this will allow you to get started with some examples.

** You will at some point run into the 'Macro Warning' *feature*. See these instructions to turn it off: http://www.mrexcel.com/archive/VBA/2432.html

Regards,
- ABT


------------------------------------
WF Environment:
------------------------------------
Server/Client, ReportCaster, Dev Studio: 7.6.11
Resource Analyzer, Resource Governor, Library, Maintain, InfoAssist
OS: Windows Server 2003
Application/Web Server: Tomcat 5.5.25
Java: JDK 1.6.0_03
Authentication: LDAP, MRREALM Driver
Output: PDF, EXL2K, HTM

------------------------------------
Databases:
------------------------------------
Oracle 10g
DB2 (AS/400)
MSSQL Server 2005
Access/FoxPro
 
Posts: 561 | Registered: February 03, 2010Reply With QuoteReport This Post
Guru
posted Hide Post
Sub SetPrintArea()
Dim PrintA As Range
Dim c As Long
'find the last used column in the range (Columns.Count)
'by working from the extreme right, the true last column is found,
'avoiding possible empty cells within that row
c = ActiveSheet.Cells(21, Columns.Count).End(xlToLeft).Column
'this will start at A21
Set PrintA = ActiveSheet.Range(Cells(21, 1), Cells(Rows.Count, c).End(xlUp))
' PrintA.PrintPreview
End Sub

Sub PrintSetup()
'
' PrintSetup Macro
' Macro recorded 10/22/2010 by Sanford Kadish
'
'
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$20"
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0.25)
.BottomMargin = Application.InchesToPoints(0.25)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = -3
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 200
.PrintErrors = xlPrintErrorsDisplayed
End With
End Sub

No guarantees but it does work over here.

Regards to the Focus Nation, Big Grin [

This message has been edited. Last edited by: S.J. Kadish,


Sandy Kadish
Dev: 8.2.04- PostgreSQL
Test: 8.2.04 - PostgreSQL
Prod: 8.2.04 - PostgreSQL
 
Posts: 239 | Location: Orlando, FL / Baltimore, MD | Registered: May 06, 2003Reply With QuoteReport This Post
Master
posted Hide Post
Ah, the old macro recorder trick. Good idea. I tend to try to solve problems the hard way. Hope it works as intended.

- ABT


------------------------------------
WF Environment:
------------------------------------
Server/Client, ReportCaster, Dev Studio: 7.6.11
Resource Analyzer, Resource Governor, Library, Maintain, InfoAssist
OS: Windows Server 2003
Application/Web Server: Tomcat 5.5.25
Java: JDK 1.6.0_03
Authentication: LDAP, MRREALM Driver
Output: PDF, EXL2K, HTM

------------------------------------
Databases:
------------------------------------
Oracle 10g
DB2 (AS/400)
MSSQL Server 2005
Access/FoxPro
 
Posts: 561 | Registered: February 03, 2010Reply With QuoteReport This Post
  Powered by Social Strata  
 

Focal Point    Focal Point Forums  Hop To Forum Categories  WebFOCUS/FOCUS Forum on Focal Point     <SOLVED Macro to automatically print out an Excel

Copyright © 1996-2018 Information Builders, leaders in enterprise business intelligence.