Controlling Print with VBA

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Controlling Print with VBA

Post by Don Wells »

Running Office 2007 on Windows 7

I have a spreadsheet which I run daily that consists of a number of images grouped together. The size of the grouped image is always two pages wide and one or two pages high.

I have been trying (without success), to develop some code to print only the left-hand pages.

Any help will be greatly appreciated.
Regards
Don

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Controlling Print with VBA

Post by Rudi »

Hi Don,

I found this code that will print every second page starting at the page you specify until the last page of the sheet.
If you specify Startpage as 1, it will print all ODD pages, conversely, 2 will print EVEN pages.

I don't know if this is exactly what you need, by try it out and see if it can be used?

Code: Select all

Sub PrintOddEven()
Dim TotalPages As Long
Dim StartPage As Long
Dim Page As Integer
    Application.PrintCommunication = False
    ActiveSheet.PageSetup.Order = xlOverThenDown
    Application.PrintCommunication = True
    StartPage = InputBox("Enter starting page number.", , 1)
    TotalPages = Application.ExecuteExcel4Macro("GET.DOCUMENT(50)")
    If StartPage > 0 And StartPage <= TotalPages Then
        For Page = StartPage To TotalPages Step 2
            ActiveSheet.PrintOut From:=Page, To:=Page, Copies:=1, Collate:=True
        Next
    End If
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Controlling Print with VBA

Post by Rudi »

Please note that I added a Print order to the macro (the first three lines)...
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Got it

Post by Don Wells »

Code: Select all

Option Explicit

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim lr As Long
Dim pic As Shape
Set pic = ActiveSheet.Shapes(1)

  lr = pic.BottomRightCell.Row
  ActiveSheet.PageSetup.PrintArea = "$A$1:$J$" & lr
  
End Sub
Thanks for the further insight Rudi.
Regards
Don