• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Print to PDF

Portucale

Member
Hi,

I have a code which "prints to a PDF" document, but for some reason it prints the completed workbook when I just wanted to print selected sheets, what do I have wrong :'(

Code:
Private Sub PrintToPDF()
Dim strReportName As String
Dim strDataPath As String
Dim wb As Workbook

strReportName = "WR0023752 - WorkStack Deck.pdf"
strDataPath = "C:\DEVELOPER_FEEDS\WR0023752\"
Set wb = ActiveWorkbook

    Sheets("Main").Activate
    With ActiveSheet
        .Shapes.Range(Array("Week")).Select
        Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = "Week commencing:  " & Range("V1").Value
        .Range("A1").Select
     End With
    Sheets(Array("Main", "Page1", "RR-Triaged", "RR-NotTriaged", "WorkInProgress", _
        "LastPage")).Select
    Sheets("Main").Activate
        'PrintOut 'Select
  
    wb.ExportAsFixedFormat Type:=xlTypePDF, FileName:=strDataPath & strReportName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=True
    Sheets("Main").Select
End Sub
Just a note, if I replace
Code:
wb.ExportAsFixedFormat Type:=xlTypePDF, FileName:=strDataPath & strReportName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=True
by
Code:
Selection.ExportAsFixedFormat Type:=xlTypePDF, FileName:=strDataPath & strReportName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
it does print the 6 sheets, but they are blank (confused)...

Any help and all help is very much appreciated.

Thanks in advance,
 
Despite being slighty unintuitive, the correct syntax would be:
Code:
 Sheets(Array("Main", "Page1", "RR-Triaged", "RR-NotTriaged", "WorkInProgress", _
        "LastPage")).Select
  
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=strDataPath & strReportName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=True

Again, one would normally think that "ActiveSheet" only refer to a single sheet, but in this case, it doesn't... o_O
 
Back
Top