Link to home
Start Free TrialLog in
Avatar of Pearlyn Tan
Pearlyn Tan

asked on

VBA Printing Error

I'm having trouble with my VBA print command - I have a module that helps me with page formatting/setup >  print worksheet. However I'm having some problems with the second half - where the first time I run this module, it doesn't apply the page formatting. I will cancel the print job, then run the module for the second time and only then will the page formatting be correct.

This part is the page setup: (rather new to VBA so please excuse if there are unnecessary lines in here!)
Sub printlist()
Dim LR As Long

With ActiveWorkbook.Sheets("Masterlist Print Format")
     LR = .Range("B" & Rows.count).End(xlUp).Row
  
    
     Application.PrintCommunication = False
    With Sheets("Masterlist Print Format").PageSetup
        .PrintArea = "A1:S" & LR
        .Orientation = xlLandscape
        
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .PrintTitleRows = "$2:$3"
        .PrintTitleColumns = ""
        .CenterFooter = "Collated Chemical Listings" & Chr(10) & "Page &P of &N"
        .PrintQuality = 600
        .CenterHorizontally = True
        .CenterVertically = False
        .Draft = False
        .PaperSize = xlPaperA4
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = True
        .AlignMarginsHeaderFooter = True
        .RightMargin = Application.InchesToPoints(0.3)
        .TopMargin = Application.InchesToPoints(0.25)
        .BottomMargin = Application.InchesToPoints(0.4)
        .HeaderMargin = Application.InchesToPoints(0.2)
        .FooterMargin = Application.InchesToPoints(0.2)
    End With

Open in new window


and the second part:
    .PrintPreview
       Application.Dialogs(xlDialogPrinterSetup).Show
  If Not (Application.Dialogs(xlDialogPrinterSetup).Show) Then Exit Sub
  ' .PrintOut From:=1, To:=2
 
End With
End Sub

Open in new window


When running the module for the first time, this is what shows on the print preview. I've also tried printing it out directly without the print preview line but the same thing is printed out. Mainly it does not set to landscape and does not fit page to 1 page wide


Subsequent attempts will give me the correct format. When i close the program and run it for the first time, it brings me back to the incorrect format.
User generated image

I've tried to google but it doesn't seem like a common problem. Hope you can help!
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pearlyn Tan
Pearlyn Tan

ASKER

Thank you!! This works!!