VBA-代码执行并产生预期的效果,但 Exportasfixedformat 行出现错误

VBA-代码执行并产生预期的效果,但 Exportasfixedformat 行出现错误

我有一个宏,用于将所有工作表导出到指定子文件夹中的单独 PDF。当我执行它时,它会在正确的位置生成 PDF 并正确命名,但随后出现错误 - 错误 5 无效的过程调用或参数 - 有什么想法吗?

    Sub SaveWorkshetAsPDF()
Application.ScreenUpdating = False

Dim ws As Worksheet
Dim path As String
Dim fol As String
Dim name As String
Dim concat As String
Dim fdObj As Object


path = ActiveWorkbook.path
fol = "\PDFs\"
Set fdObj = CreateObject("Scripting.FileSystemObject")
If fdObj.FolderExists(path & fol) Then
MsgBox "Found it", vbInformation
Else
fdObj.createfolder (path & fol)
MsgBox "folder created", vbInformation
End If

For Each ws In Worksheets


    name = ws.name
    concat = path & fol & name

    With ws.PageSetup
        .Orientation = xlLandscape
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .PaperSize = xlPaperA3
    End With
    ws.ExportAsFixedFormat xlTypePDF, fileName:=concat
Next ws

Application.ScreenUpdating = True

End Sub

答案1

添加文件扩展名以及 ActiveWorkBook

For Each ws In Worksheets


name = ws.name
concat = path & fol & name & ".pdf"

With ws.PageSetup
    .Orientation = xlLandscape
    .FitToPagesWide = 1
    .FitToPagesTall = False
    .PaperSize = xlPaperA3
End With
ActiveWorkbook.ExportAsFixedFormat xlTypePDF, Filename:=concat
Next ws

相关内容