title: Exporting excel sheet as PDF Run-time error '5': Invalid procedure call or argument
body: <p>I have a problem exporting one Excel sheet in a workbook into a PDF file.</p>
  
  <pre><code>Sub ExportPDF()
      Dim Nazwa As String
      ' Creating a destination folder for PDF file
      If Len(Dir("c:\Faktury", vbDirectory)) = 0 Then
          MkDir "c:\Faktury"
      End If
      'InputBox that allows user to type filename
      Nazwa = InputBox("Wpisz nazwę pliku", "Nazwa pliku", Sheets("Fa VAT").Range("G3"))
      If Nazwa = "" Then Exit Sub
      ChDir "c:\Faktury"
      'Exporting sheet "Fa VAT" to PDF
      Sheets("Fa VAT").ExportAsFixedFormat _
      Type:=xlTypePDF, _
      Filename:=Nazwa, _
      Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, _
      OpenAfterPublish:=True
  End Sub
  </code></pre>
  
  <p>I get the error "Run-time error '5': Invalid procedure call or argument". After that Excel highlights the last part of the code as it is supposed to be wrong. I mean exactly this part:</p>
  
  <pre><code>    Sheets("Fa VAT").ExportAsFixedFormat _
      Type:=xlTypePDF, _
      Filename:=Nazwa, _
      Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, _
      OpenAfterPublish:=True
  </code></pre>
  
  <p>I installed Microsoft SaveAsPDF setup and nothing changes. I changed filename to some random string rather than a referring to InputBox. I tried to activate the right sheet and then export ActiveSheet. I tried with the sheet hidden and shown. I tried to run the file on two other computers.</p>
  
  <p>When I copy this macro to some completely new, blank excel spreadsheet, it works.</p>
  
  <p>I work on Excel 2007, Windows 10. I tried it also on Excel 2010 and Windows 7.</p>
  
  <p>Here are some of the links that I tried:</p>
  
  <p><a href="https://stackoverflow.com/questions/14053873/runtime-error-5-invalid-procedure-call-or-argument">Runtime Error : 5, Invalid Procedure call or argument</a></p>
  
  <p><a href="http://www.mrexcel.com/forum/excel-questions/595252-visual-basic-applications-trouble-save-pdf-code.html" rel="nofollow noreferrer">http://www.mrexcel.com/forum/excel-questions/595252-visual-basic-applications-trouble-save-pdf-code.html</a></p>