0

Request your help.

I am trying to save the values in a range of cells in an active worksheet of the excel workbook that is open, as a PDF file.

I am using the following code for the same.

Dim objExcel
Dim objExcelPayrollDetailsWB
Dim objExcelEmpDetailWS
Dim objExcelPayslipWS

Dim strExcelFilePath
StrExcelFilePath = "D:\Salary\PayrollProcessing\Sample Salary Statement Sep-2019.xlsx"

Dim strPdfPaysliPath
strPdfPaysliPath = "D:\Salary\PayrollProcessing\GeneratedPayslips\"

Dim strPayslipFileNameWithPath
Dim strPaySlipName
Dim strPaySlipMonthYear

' Initiate Excel Application
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True

' Specify the Excel workbook to be opened and the logical local drive path where the workbook is available
Set objExcelPayrollDetailsWB = objExcel.Workbooks.open(strExcelFilePath)

' Set the focus to the required worksheet
Set objExcelEmpDetailWS = objExcelPayrollDetailsWB.Worksheets("BLRCC")

strPaySlipMonthYear = objExcelEmpDetailWS.Cells(5,4).Value
strPaySlipMonthYear = Month(strPaySlipMonthYear)&"-"&Year(strPaySlipMonthYear)

Set objExcelPayslipWS = objExcelPayrollDetailsWB.Worksheets("BLRCC PS")

strPaySlipName = objExcelPayslipWS.Cells(15,15).Value&"_"&strPaySlipMonthYear

strPayslipFileNameWithPath = strPdfPaysliPath & strPaySlipName & ".pdf"

' Code to save the active worksheet (the payslip worksheet in the 'PayrollDetailsWB') as PDF in a specific path
'   *SAVE Payslip as a PDF document*
'   *************************************
'   Save a range as PDF
    objExcelPayslipWS.Range("A1:AB55").Select
    MsgBox "The name of the active sheet is: " & (objExcelPayrollDetailsWB.ActiveSheet.Name)

    objExcel.Activeworkbook.Save

    objExcel.ActiveSheet.ExportAsFixedFormat 0, Filename:= _
        strPayslipFileNameWithPath, Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

When I execute it, a Microsoft VBScript compilation error, stating,

"Expected statement" is thrown at the line, objExcel.ActiveSheet.ExportAsFixedFormat 0, Filename:= _

3
  • VBScript doesn't have named parameters, you can't do Filename:=.
    – BigBen
    Dec 19, 2019 at 15:53
  • Does this answer your question? Does VBScript allow named arguments in function calls?
    – BigBen
    Dec 19, 2019 at 15:53
  • 1
    Thank you Ben. The following script finally worked. 'objExcelPayslipWS.Range("A1:AB55").ExportAsFixedFormat 0, strPayslipFileNameWithPath,0, 1, 0,,,0' Dec 20, 2019 at 16:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.