将多个Excel工作表导出到单独的PDF中会出现运行时错误5

z31licg0  于 2023-02-17  发布在  其他
关注(0)|答案(2)|浏览(178)

我需要创建一个宏,它将每个工作表导出到一个单独的PDF中,并将工作表的名称作为PDF的新文件名。当我运行该宏时,它会吐出运行时错误5:无效的过程调用或参数
她就是我的密码:

Sub Makro1()

Dim Folder_Path As String
With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Ordner zum Speichern der PDFs auswählen"
    
    If .Show = -1 Then Folder_Path = .SelectedItems(1)
    
End With

Dim sh As Worksheet

For Each sh In ActiveWorkbook.Worksheets

    sh.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Folder_Path & Application.PathSeparator & sh.Name & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Next

MsgBox "Fertig!"

End Sub

它似乎在ExportAsFixedFormat操作上遇到了错误,但我不知道为什么。

ct3nt3jp

ct3nt3jp1#

请尝试下一个修改后的代码。它之前检查了工作表的可见性,并根据其状态,从这个Angular 来看,取消隐藏,导出和隐藏它,因为它最初是:

Sub Makro1()
 Dim Folder_Path As String

 With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Ordner zum Speichern der PDFs auswählen"
    
    If .Show = -1 Then Folder_Path = .SelectedItems(1)
 End With

 Dim sh As Worksheet, shVis As XlSheetVisibility, boolVis As Boolean

 For Each sh In ActiveWorkbook.Worksheets
    With sh
        If .Visible <> xlSheetVisible Then   'if it is hidden
            shVis = .Visible                            'memorise its visibility
            sh.Visible = xlSheetVisible: boolVis = True 'make it visible and boolVis True
        End If
        'export the (visible) sheet:
        .ExportAsFixedFormat Type:=xlTypePDF, fileName:=Folder_Path & _
                Application.PathSeparator & sh.name & ".pdf", Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
                
        If boolVis Then .Visible = shVis: boolVis = False 'change back the initial visibility and reinitialize boolVis as False
    End With
 Next

MsgBox "Fertig!"

End Sub

未测试,但应解决隐藏工作表问题。
如果仍然出现问题,请进行同样的检查,将光标移动到相应的工作表名称上,查看问题所在。例如,如果受保护的工作表,代码也可以调整以克服这种情况...

wfveoks0

wfveoks02#

有可能你的一张纸是空的

相关问题