excel 运行时错误“5”:无效的过程调用或参数

kq4fsx7k  于 2023-01-21  发布在  其他
关注(0)|答案(2)|浏览(2298)

我以前用过下面的代码,并按预期工作了几次。4小时后,它没有工作。我添加了**MsgBox“文件:“**并确认文件名路径无错误。

Option Explicit

Sub ExportAsPDF()

Dim Folder_Path As String

Dim NameOfWorkbook

NameOfWorkbook = Left(ActiveWorkbook.Name, (InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare) - 1))

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select Folder path"
    
    If .Show = -1 Then Folder_Path = .SelectedItems(1)

End With

If Folder_Path = "" Then Exit Sub

Dim sh As Worksheet
Dim fn As String

For Each sh In ActiveWorkbook.Worksheets
    fn = Folder_Path & Application.PathSeparator & NameOfWorkbook & "_" & sh.Name & ".pdf"
    MsgBox "File: " & fn
    sh.PageSetup.PaperSize = xlPaperA4
    sh.PageSetup.LeftMargin = Application.InchesToPoints(0.5)
    sh.PageSetup.RightMargin = Application.InchesToPoints(0.5)
    sh.PageSetup.TopMargin = Application.InchesToPoints(0.5)
    sh.PageSetup.BottomMargin = Application.InchesToPoints(0.5)
    sh.PageSetup.HeaderMargin = Application.InchesToPoints(0.5)
    sh.PageSetup.FooterMargin = Application.InchesToPoints(0.5)
    sh.PageSetup.Orientation = xlPortrait
    sh.PageSetup.CenterHorizontally = True
    sh.PageSetup.CenterVertically = False
    sh.PageSetup.FitToPagesTall = 1
    sh.PageSetup.FitToPagesWide = 1
    sh.PageSetup.Zoom = False
    sh.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fn, Quality:=xlQualityStandard, OpenAfterPublish:=True

Next

MsgBox "Done"

End Sub

有什么我错过了吗?

  • 适用于Microsoft 365 MSO的Microsoft® Excel®(版本2211内部版本号16.0.15831.20220)64位 *
pcrecxhr

pcrecxhr1#

如果ActiveWorkbook是新的,并且从未存储过,则工作簿名称是 * 不带 * 任何扩展名的通用名称,例如Book1。在这种情况下,InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare)将返回0。因为您嵌套了两个命令,所以此0将作为参数传递给Left函数,并且Left(Name, 0)将引发运行时错误5。
解决方法:将InstrRev的结果写入中间变量并检查它。我的建议是避免嵌套命令,因为如果因为0是无效参数而出现错误,则很难检查到底是什么失败了。

Dim p As Long
p = InStrRev(ActiveWorkbook.Name, ".")
If p = 0 Then
    NameOfWorkbook = ActiveWorkbook.Name
Else
    NameOfWorkbook = Left(ActiveWorkbook.Name, p - 1)
End If

获取不带扩展名的文件名的另一种方法是使用FileSystemObject-方法GetBaseName(在Mac上不起作用)

nameOfWorkbook = CreateObject("Scripting.fileSystemObject").GetBasename(ActiveWorkbook.FullName)
ogsagwnx

ogsagwnx2#

将工作表导出到单个PDF

  • 只有当工作表不可见(隐藏或非常隐藏)时,我才会产生错误。下面将讨论这个问题和其他一些问题。
Sub ExportAsPDF()
    
    Const PROC_TITLE As String = "Export As PDF"
    Const EXPORT_ONLY_VISIBLE_WORKSHEETS As Boolean = False
    
    If ActiveWorkbook Is Nothing Then
        MsgBox "No visible workbooks open.", vbExclamation, PROC_TITLE
        Exit Sub
    End If
    
    Dim swb As Workbook: Set swb = ActiveWorkbook
    If Len(swb.Path) = 0 Then
        MsgBox "The workbook was not saved yet." & vbLf & vbLf _
            & "Save it and try again.", vbExclamation, PROC_TITLE
        Exit Sub
    End If
    
    Dim dFolderPath As String

    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select Folder path"
        If .Show Then dFolderPath = .SelectedItems(1)
    End With
    
    If Len(dFolderPath) = 0 Then
        MsgBox "No folder selected.", vbExclamation, PROC_TITLE
        Exit Sub
    End If
    
    Dim swbBaseName As String: swbBaseName = swb.Name
    swbBaseName = Left(swbBaseName, InStrRev(swbBaseName, ".") - 1)

    Dim dFilePathLeft As String
    dFilePathLeft = dFolderPath & Application.PathSeparator & swbBaseName & "_"
    
    Dim sVisibility As XlSheetVisibility: sVisibility = xlSheetVisible
    
    Dim sws As Worksheet
    Dim dCount As Long
    Dim dFilePath As String
    Dim DoExport As Boolean

    For Each sws In swb.Worksheets
        
        With sws
            
            If EXPORT_ONLY_VISIBLE_WORKSHEETS Then ' only visible
                If .Visible = xlSheetVisible Then DoExport = True
            Else ' all
                If Not .Visible = xlSheetVisible Then
                    sVisibility = .Visible ' store
                    .Visible = xlSheetVisible ' make visible
                End If
                DoExport = True
            End If
            
            If DoExport Then
                With .PageSetup
                    .PaperSize = xlPaperA4
                    .LeftMargin = Application.InchesToPoints(0.5)
                    .RightMargin = Application.InchesToPoints(0.5)
                    .TopMargin = Application.InchesToPoints(0.5)
                    .BottomMargin = Application.InchesToPoints(0.5)
                    .HeaderMargin = Application.InchesToPoints(0.5)
                    .FooterMargin = Application.InchesToPoints(0.5)
                    .Orientation = xlPortrait
                    .CenterHorizontally = True
                    .CenterVertically = False
                    .FitToPagesTall = 1
                    .FitToPagesWide = 1
                    .Zoom = False
                End With
                dFilePath = dFilePathLeft & .Name & ".pdf"
                .ExportAsFixedFormat Type:=xlTypePDF, Filename:=dFilePath, _
                    Quality:=xlQualityStandard, OpenAfterPublish:=True
                dCount = dCount + 1
                DoExport = False ' reset for the next iteration
            End If
            
            If Not EXPORT_ONLY_VISIBLE_WORKSHEETS Then ' all
                If Not sVisibility = xlSheetVisible Then
                    .Visible = sVisibility ' revert
                    sVisibility = xlSheetVisible ' reset
                End If
            End If
            
        End With
    
    Next sws

    MsgBox dCount & " worksheet" & IIf(dCount = 1, "", "s") & " exported.", _
        vbInformation, PROC_TITLE

End Sub

相关问题