Search code examples
excelvbaexcel-2007

Exporting sheet template to PDF generates Runtime Error 5: Invalid Procedure call or argument


I have created a macro to export the sheet template to PDF:

ActiveWorkbook.Sheets("Sheet2").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
  ActiveWorkbook.Path & "\Survey Report.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
  :=False, OpenAfterPublish:=True

I tried it other ways but it makes no difference in error.

Links already tried:

  1. http://www.mrexcel.com/forum/excel-questions/608810-printing-multiple-worksheets-pdf-2.html
  2. http://www.mrexcel.com/forum/microsoft-access/385749-exportasfixedformat-error.html
  3. http://answers.microsoft.com/en-us/office/forum/office_2010-customize/error-5-invalid-procedure-call-or-argument/574c2c8f-7f2c-4644-9373-bbc14c8d3fd7?msgId=4dbee3f3-ad28-4427-a50b-a3904b09ec1e

Solution

  • Your actual code works for me. However try this (TRIED AND TESTED)

    Sub Sample()
        ActiveWorkbook.Sheets("Sheet2").Activate
    
        ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=ActiveWorkbook.Path & "\Survey Report.pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
    End Sub
    

    Note:

    1. Also hope Sheet2 has something in it to print else a blank sheet will give you the error.
    2. If you want to print the sheet from the workbook where you are running the code then you might want to change ActiveWorkbook to ThisWorkbook and try again...