Attribute VB_Name = "ArticleExamples"
Option Explicit

Public Sub ExportReportPdf()
    Dim ws As Worksheet
    Dim folder As String
    Dim target As String
    On Error GoTo Failed

    If InStr(1, Application.OperatingSystem, "Mac", vbTextCompare) > 0 Then
        Err.Raise vbObjectError + 1005, , "This example requires Windows desktop Excel."
    End If
    If ThisWorkbook.Path = "" Or InStr(ThisWorkbook.Path, "://") > 0 Then
        Err.Raise vbObjectError + 1000, , "Save a local copy of this workbook first."
    End If
    Set ws = ThisWorkbook.Worksheets("Report")
    If ws.PageSetup.PrintArea = "" Then
        Err.Raise vbObjectError + 1001, , "Set and preview the Report print area first."
    End If
    folder = ThisWorkbook.Path & "\PDF"
    If Dir(folder, vbDirectory) = "" Then MkDir folder
    target = folder & "\Report_" & Format(Now, "yyyymmdd_hhnnss") & ".pdf"
    If Len(Dir$(target, vbNormal Or vbReadOnly Or vbHidden Or vbSystem Or vbDirectory)) > 0 Then
        Err.Raise vbObjectError + 1002, , "Target already exists. No overwrite attempted."
    End If

    ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=target, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=False
    If Dir(target) = "" Then Err.Raise vbObjectError + 1003, , "PDF was not created."
    If FileLen(target) = 0 Then Err.Raise vbObjectError + 1004, , "PDF is empty."
    MsgBox "Open the PDF and check pages and content: " & target, vbInformation
    Exit Sub
Failed:
    MsgBox "Export stopped. Error " & Err.Number & ": " & Err.Description, vbExclamation
End Sub
