+ Reply to Thread
Results 1 to 2 of 2

VBA Print & Page numbers

Hybrid View

  1. #1
    Registered User
    Join Date
    06-04-2010
    Location
    Bolton, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    VBA Print & Page numbers

    Hi all,

    I'm having a real tough time at the minute trying to get the correct page numbers to display on an excel file that is ultimately printed as a .pdf document.

    I have a workbook containing 30 sheets that are printed into a .pdf document. Within the document I have some code that prints each sheet seperately and places a page number on the page.

    Here's the code:

    Sub ExportOutput()
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Dim outputJob As PDFCreator.clsPDFCreator
    Dim sThisBook As String
    Dim sThisSheet As String
    Dim sOutputFile As String
    Dim sOutputPath As String
    Dim lOutputFormat As Long
    Dim lSheetCount As Long
    Dim sTargetSheet As String
    Dim pageNumber As Long
    Dim iHpBreaks As Integer
    Dim iVBreaks As Integer
    Dim iTotPages As Integer
    
    sThisBook = ActiveWorkbook.Name
    sThisSheet = ActiveSheet.Name
    
    Windows(sThisBook).Activate
    Sheets(sThisSheet).Select
    Range("A1").Select
    
    sOutputFile = Range("TargetFilename").Text
    sOutputPath = Range("TargetFilepath").Text
    lOutputFormat = Application.VLookup(Range("OutputType").Text, Range("OutputLookup"), 3, 0)
    lSheetCount = Range("SheetsToPrint").Cells.Count
    pageNumber = 1
    
    Set outputJob = Nothing
    
    Set outputJob = New PDFCreator.clsPDFCreator
    
    With outputJob
    
        If .cStart("/NoProcessingAtStartup") = False Then
            MsgBox "Can't initialize PDFCreator.", vbCritical + _
                    vbOKOnly, "PrtPDFCreator"
        Exit Sub
    
        End If
    
        .cOption("UseAutosave") = 1
        .cOption("UseAutosaveDirectory") = 1
        .cOption("AutosaveDirectory") = sOutputPath
        .cOption("AutosaveFilename") = sOutputFile
        .cOption("AutosaveFormat") = lOutputFormat
        .cClearCache
    
    End With
    
    For Each cell In Range("SheetsToPrint")
    
    sTargetSheet = cell.Text
    
    
    iHpBreaks = Sheets(sTargetSheet).HPageBreaks.Count + 1
    iVBreaks = Sheets(sTargetSheet).VPageBreaks.Count + 1
    iTotPages = iHpBreaks * iVBreaks
    
    If Sheets("Control").Range("C" & cell.Row).Text = "x" Then
    
    Application.Sheets(sTargetSheet).PageSetup.RightFooter = "&""Arial"" &11" & "Page " & pageNumber
    Application.Sheets(sTargetSheet).PageSetup.LeftFooter = "&""Arial"" &11" & sOutputFile
    
    Else
    
    Application.Sheets(sTargetSheet).PageSetup.RightFooter = "&""Arial"" &15" & "Page " & pageNumber
    Application.Sheets(sTargetSheet).PageSetup.LeftFooter = "&""Arial"" &15" & sOutputFile
    
    End If
    
    Application.Sheets(sTargetSheet).PrintOut copies:=1, ActivePrinter:="PDFCreator"
    Application.Wait Now + TimeValue("0:0:3")
    
    Application.Sheets(sTargetSheet).PageSetup.RightFooter = ""
    Application.Sheets(sTargetSheet).PageSetup.LeftFooter = ""
    
    pageNumber = pageNumber + iTotPages
    
    Next cell
    
    Do Until outputJob.cCountOfPrintjobs = lSheetCount
        DoEvents
    Loop
    
    With outputJob
        .cCombineAll
        .cPrinterStop = False
    End With
    
    Do Until outputJob.cCountOfPrintjobs = 0
        DoEvents
    Loop
    
    Application.Wait Now + TimeValue("0:0:55")
    
    With outputJob
        .cPrinterStop = True
        .cClose
    End With
    
    Set outputJob = Nothing
    
    Windows(sThisBook).Activate
    Sheets(sThisSheet).Select
    Range("A1").Select
    
    End Sub
    The problem I have is when one of the sheets is displayed over two or more pages. The code puts the same page number on each of the sheets. I've been searching high and low to find something which follows this logic;

    If pagecount of ("sheeta") = 2 Then
    
    Print page 1 of 2 (placing page number on the sheet)
    
    &
    
    Print page 2 of 2 (placing page number + 1 on the sheet)
    
    Else continue with my above code
    Anyone got any idea's on how I can overcome this?

    Thanks,
    Lee

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: VBA Print & Page numbers

    how about changing your appraoch slightly and selecting all of the worksheets using something like Sheets(Array("A","B","C").Select and then using ActiveWindow.SelectedSheets.PrintOut. Then Printout will keep track of page numbering.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1