我在excel中的宏vba脚本中得到运行时错误1004



你能帮我解决这个问题吗

我正在为某一范围的对象制作一个宏,将文件保存为excel中的pdf格式,但遇到了运行时错误

我收到一个错误:

运行时错误1004:"文档未保存,或者可能打开,或者保存时出错">

下面是它的代码:

Sub AntigenReportSlip()
Sheets("AntigenReportSlip").Select
Dim filename As String
Dim ChDir As String


filename = Range("E9")
ChDir = "D:New Lab ReportAntigenReports"
Sheets("AntigenReportSlip").Range("$A$1:$U$33").ExportAsFixedFormat Type:=xlTypePDF,
filename:= _
ChDir & filename & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False,
openAfterPublish:=True
End Sub

据我所知,Excel的Range类没有ExportAsFixedFormat方法。但是,工作簿类确实如此。

这应该会帮助你

Sub AntigenReportSlip()

' Make sure this folder already exists
Dim OutputFolder As String
OutputFolder = "D:New Lab ReportAntigenReports"
' Get filename from E9 cell
Dim filename As String
filename = Sheets("AntigenReportSlip").Range("E9").value

' Compute full filename.
' Note: You should always store fullpath on a variable before using it.
Dim fullFilename As String
fullFilename = OutputFolder & filename
' Select cells range you would like to export
Sheets("AntigenReportSlip").Range("$A$1:$U$33").Select

' Export selected range to PDF e present it
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, filename:=fullFilename, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, openAfterPublish:=True
End Sub

有关ExportAsFixedFormat方法的更多详细信息,请检查https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat

最新更新