Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
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




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.
Posted
Updated 15-Sep-17 0:03am
v2

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?
 
Share this answer
 
v2
Not your question, but this line
VB
lastRow = Sheets("Invoice File").UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1

can be simplified to
VB
lastRow = Sheets("Invoice File").UsedRange.RowS.Count + 1
 
Share this answer
 
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)[^]
There may be different error codes for some of the above reasons but you should check them all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900