• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Excel to PDF Issue

KunalTalreja

New Member
Hi,

I am facing problem in vba coding as Run time Error 5,

It gives me the error when converting from excel to pdf

Below is the script

[pre]
Code:
Option Explicit

Sub Slip()

Dim Cell As Range
Dim Curpath As String
Dim PSFileName As String

Curpath = ActiveWorkbook.Path & ""

For Each Cell In Range("Name")
[salary] = Cell.Value
If (Range("salary")) = Empty Then
Exit Sub

End If

ActiveSheet.Range("A7:E42").ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "salaryslip-" & Format(Now, "ddmmyyyy-hhmm") & ".pdf"

Next Cell

End Sub
[/pre]
 
Shouldn't it be Curpath&"salarylsip"?


I've done some VBA, but I'm just a novice. It seems you've built a variable near the top, then left it to flounder. Just my stab in the dark....
 
As DonMinter points out, you should use the variable probably. This code works for me.

[pre]
Code:
Option Explicit

Sub Slip()

Dim C As Range
Dim Curpath As String
Dim PSFileName As String

Curpath = ActiveWorkbook.Path & ""

For Each C In Range("Name")
[salary] = C.Value
If (Range("salary")) = Empty Then
Exit Sub
End If

PSFileName = Curpath & "salaryslip-" & Format(Now, "ddmmyyyy-hhmm") & ".pdf"
ActiveSheet.Range("A7:E42").ExportAsFixedFormat xlTypePDF, PSFileName
Next C

End Sub
[/pre]
Do note, that since this won't take too long to run through, if there are multiple cells in Range("Name"), you will be overwriting your files since file name only includes minutes and seconds. Might need to add the seconds to it, or some other unique identifier.
 
Back
Top