0
votes

I have this code:

Sub PrintPDF()



Dim wsReport As Worksheet
Dim confirm As Long
Dim filename, reportsPath As String
Dim printArea As Range

Set wsReport = ThisWorkbook.Worksheets("Test Status")
Set printArea = wsReport.Range("A1:AG80")

'Generate Reports folder path
'reportsPath = ThisWorkbook.Path & "\Reports\"
reportsPath = "C:\"
'Generate filename to be printed
Dim LValue As String

LValue = Format(Date, "yyyymmdd")


fp = reportsPath & Range("Project!clientName").Value & "_TestReport_" & LValue & ".pdf"

'Confirm or Cancel the action
confirm = MsgBox("the Test execution report (" & fp & ") will be printed as PDF in the folder " & reportsPath & " .", vbOKCancel + vbQuestion, "Printing Test report")
If confirm = vbCancel Then
    Exit Sub
End If

'Set page orientation to landscape
wsReport.PageSetup.Orientation = xlLandscape
'wsReport.PageSetup.Orientation = xlPortrait
Application.ScreenUpdating = False

With ActiveSheet.PageSetup
    .printArea = Worksheets("Test Status").UsedRange
    '.printArea = wsReport.UsedRange
    '.printArea = Worksheets("Test Status").UsedRange
    .Orientation = xlLandscape
    .FitToPagesWide = 1
    .Zoom = False 'I have added this line
End With

printArea.ExportAsFixedFormat Type:=xlTypePDF, filename:=fp, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True



End Sub

This code is activated when the button on the sheet is pressed.

Once I press the button, I got the message box with the filename and path where the file will be stored.

once pressed on "OK" button I got this error:

Run-Time Error '1004' Document not saved. The document may be open or an error may have been encountered

I'm using Office 365

1

1 Answers

0
votes

fixed, seems that the issue was caused by directory grant.

Btw, I changed the reportsPath

reportsPath = ThisWorkbook.Path & "\Reports\"

And I've added the check to create the directory in case it doesn't exist.

reportsPath = ThisWorkbook.Path & "\Reports\"
If Dir(reportsPath, vbDirectory) = "" Then
    create = MsgBox("The Directory " & reportsPath & " doesn't exist. ", vbOKCancel + vbQuestion, "Do you want to create it?")
    If create = vbCancel Then
        Exit Sub
    End If
    MkDir reportsPath

    Stop
End If

Now it works, no runtime error anymore.