我有这段代码:
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
按下工作表上的按钮时会激活此代码。
按下按钮后,我收到了消息框,其中包含存储文件的文件名和路径。
一旦按下" OK"按钮我收到此错误:
运行时错误' 1004'文档未保存。该文件可能是开放的或 可能遇到错误
我正在使用Office 365
答案 0 :(得分:0)
固定, 似乎问题是由目录授予引起的。
顺便说一句,我更改了reportsPath
reportsPath = ThisWorkbook.Path & "\Reports\"
我已经添加了检查以创建目录,以防它不存在。
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
现在它可以工作,不再有运行时错误。