运行时错误低于代码 [英] Run time error below code

查看:104
本文介绍了运行时错误低于代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sub InvoiceReport()

    Dim myFile As String, lastRow As Long

    myFile = "C:\Users\iaquino\Documents\Invoices\" & _ 
        Sheets("Invoice").Range("A10") & "_" & _
        Sheets("Invoice").Range("F4") & _
        Format(Now(), "yyy-mm-dd") & ".pdf"

    lastRow = Sheets("Invoice File").UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1

    'Transfer data to Invoice File
    Sheets("Invoice File").Cells(lastRow, 1) = Sheets("Invoice").Range("A10")
    Sheets("Invoice File").Cells(lastRow, 2) = Sheets("Invoice").Range("F4")
    Sheets("Invoice File").Cells(lastRow, 3) = Sheets("Invoice").Range("F28")
    Sheets("Invoice File").Cells(lastRow, 4) = Now
    Sheets("Invoice File").Hyperlinks.Add Anchor:=Sheets("Invoice File").Cells(lastRow, 5), Address:=myFile, TextToDisplay:=myFile

    'Create invoice in PDF format
    Sheets("Invoice").ExportAsFixedFormat Type:=xlTypePDF, Filename:=myFile

    Application.DisplayAlerts = False

    'create invoice in XLSX format
    ActiveWorkbook.SaveAs "C:\Users\iaquino\Documents\invoices\" & _
        Sheets("Sheet1").Range("A10") & "_" & _
        Sheets("Invoice").Range("F4") & "_" & _
        Format(Now(), "yyy-mm-dd") & "xlsx", FileFormat:=51

    Application.DisplayAlerts = True

End Sub









调试错误: -

运行时错误'5'





表格(发票)。ExportAsFixedFormat类型:= xlTypePDF,文件名:= myFile

Application.DisplayAlerts = False



我的尝试:



Plz紧急解决这个问题。





Error debug:-
Run time error '5'


Sheets("Invoice").ExportAsFixedFormat Type:=xlTypePDF, Filename:=myFile
Application.DisplayAlerts = False

What I have tried:

Plz give urgent solution this problem.

推荐答案

快速Google搜索Excel error 5会返回:无效的过程调用或参数(错误5) [ ^ ]



所以你需要弄清楚在哪里。要执行此操作,请在第一行上设置断点并逐步执行,直到出现错误行。现在您知道错误的位置,现在您已准备好修复它。



如果实际上它是您已识别的线路,那么您需要检查功能要求: ExportAsFixedFormat方法  [Excel 2007开发人员参考] [ ^ ] 。猜测,路径& /或文件名可能有问题。这条路是否存在? myFile 是否有效路径+文件名?
A quick Google Search of "Excel error 5" returns this: Invalid procedure call or argument (Error 5)[^]

So you now need to figure out where. To do this, set a breakpoint on the first line and step through until you hit the line with the error. Now you know where the error is and now you are ready to fix it.

If in fact it is the line that you have identified, then you need to check the function requirements: ExportAsFixedFormat Method [Excel 2007 Developer Reference][^]. At a guess, there may be a problem with the path &/or filename. Does the path exist? Is the myFile a valid path+filename?


不是你的问题,但这一行

Not your question, but this line
lastRow = Sheets("Invoice File").UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1



可以简化为


can be simplified to

lastRow = Sheets("Invoice File").UsedRange.RowS.Count + 1


如果在调用 ExportAsFixedFormat 时发生,可能有以下原因:



  • 未安装PDF加载项

  • 路径 C:\Users\iaquino \Documents\Invoices\ 不存在

  • 您没有足够的权限来写以上路径(未作为用户 iaquino 执行)

  • 创建的文件na me(单元格A10和/或F4)包含保留字符;请参阅命名文件,路径和命名空间(Windows) [ ^ ]

If it occurs when calling ExportAsFixedFormat, there may be these reasons :

  • The PDF add-in is not installed
  • The path C:\Users\iaquino\Documents\Invoices\ does not exist
  • You have insufficient privileges to write the above the path (not executed as user iaquino)
  • The created file name (the cells A10 and/or F4) contain reserved characters; see Naming Files, Paths, and Namespaces (Windows)[^]


这篇关于运行时错误低于代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆