excelpdfvbscript

Convert excel to pdf with VBScript


I want to convert excel to pdf with vbscript but it gives me an error following is my code:

Option Explicit
Dim objExcel, strExcelPath, objSheet

strExcelPath = "path\file.xlsx"

' Open specified spreadsheet and select the first worksheet.
Set objExcel = CreateObject("Excel.Application")
objExcel.WorkBooks.Open strExcelPath
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

' Modify a cell.
objSheet.Range("B8").FormulaR1C1  = [sample1]
objSheet.Range("G11").FormulaR1C1 = [sample2]
objSheet.Range("G12").FormulaR1C1 = [sample3]
objSheet.Range("B10").FormulaR1C1 = [sample4]
objSheet.Range("B11").FormulaR1C1 = [sample5]

' Save as Excel.
objExcel.ActiveWorkbook.SaveAs "path\name.xlsx" 
' Problem in saving to pdf
objExcel.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:= "path\name.pdf", _
        Quality:= xlQualityStandard, IncludeDocProperties:=True,_
        IgnorePrintAreas:=False, OpenAfterPublish:=False

objExcel.ActiveWorkbook.Close
objExcel.Application.Quit

If you find any imporovement in code also, please tell me.


Solution

  • In case somebody else has this issue replace the:

    objExcel.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
            Filename:= "path\name.pdf", _
            Quality:= xlQualityStandard, IncludeDocProperties:=True,_
            IgnorePrintAreas:=False, OpenAfterPublish:=False
    

    With:

    objExcel.ActiveSheet.ExportAsFixedFormat 0, "path\name.pdf" ,0, 1, 0,,,0
    

    This way it works!