Focal Point
<SOLVED Macro to automatically print out an Excel

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/6617051295

October 21, 2010, 08:14 PM
S.J. Kadish
<SOLVED Macro to automatically print out an Excel
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
October 21, 2010, 08:33 PM
Waz
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.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

October 22, 2010, 08:47 AM
ABT
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
October 22, 2010, 11:38 AM
S.J. Kadish
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
October 22, 2010, 11:51 AM
ABT
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