hi team,
my code is not running in workbook but if i m running on script page with pressing F8 to run code its working correctly. kindly please help me. on this please see my code below:
Sub PBFINV()
Dim wb As Workbook
Dim wb1 As Workbook
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String
Dim bRestart As Boolean
Set wb = Workbooks("book1.xlsx")
Set wb1 = Workbooks("PBF Invoice template.xlsx")
Set ws = wb.Worksheets("sheet1")
Set ws1 = wb1.Worksheets("Template to fill up (protect2)")
Set ws3 = wb1.Worksheets("Invoice copy (protect)")
wb.Activate
Range("E2").Activate
NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count
For q = 1 To NumRows
If Not IsEmpty(ActiveCell.Value) Then
ws1.Range("B20").Value = ActiveCell.Value
ws1.Range("B25").Value = ActiveCell.Offset(0, -2).Value
ws1.Range("B7").Value = ActiveCell.Offset(0, -3).Value
ws1.Range("B6").Value = ActiveCell.Offset(0, -4).Value
sPDFName = ActiveCell.Value
sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
'Check if worksheet is empty and exit if so
If IsEmpty(ws.UsedRange) Then Exit Sub
Set pdfjob = New PDFCreator.clsPDFCreator
'Check if PDFCreator is already running and attempt to kill the process if so
Do
bRestart = False
Set pdfjob = New PDFCreator.clsPDFCreator
If pdfjob.cStart("/NoProcessingAtStartup") = False Then
'PDF Creator is already running. Kill the existing process
Shell "taskkill /f /im PDFCreator.exe", vbHide
DoEvents
Set pdfjob = Nothing
bRestart = True
End If
Loop Until bRestart = False
'Assign settings for PDF job
With pdfjob
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Delete the PDF if it already exists
If Dir(sPDFPath & sPDFName) = sPDFName Then Kill (sPDFPath & sPDFName)
'Print the document to PDF
ws3.PrintOut copies:=1, ActivePrinter:="PDFCreator"
'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
ActiveCell.Offset(1, 0).Activate
If Not IsEmpty(ActiveCell.Value) Then
ws1.Range("B20").Value = ActiveCell.Value
ws1.Range("B25").Value = ActiveCell.Offset(0, -2).Value
ws1.Range("B7").Value = ActiveCell.Offset(0, -3).Value
ws1.Range("B6").Value = ActiveCell.Offset(0, -4).Value
End If
End If
Next
End sub
my code is not running in workbook but if i m running on script page with pressing F8 to run code its working correctly. kindly please help me. on this please see my code below:
Sub PBFINV()
Dim wb As Workbook
Dim wb1 As Workbook
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String
Dim bRestart As Boolean
Set wb = Workbooks("book1.xlsx")
Set wb1 = Workbooks("PBF Invoice template.xlsx")
Set ws = wb.Worksheets("sheet1")
Set ws1 = wb1.Worksheets("Template to fill up (protect2)")
Set ws3 = wb1.Worksheets("Invoice copy (protect)")
wb.Activate
Range("E2").Activate
NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count
For q = 1 To NumRows
If Not IsEmpty(ActiveCell.Value) Then
ws1.Range("B20").Value = ActiveCell.Value
ws1.Range("B25").Value = ActiveCell.Offset(0, -2).Value
ws1.Range("B7").Value = ActiveCell.Offset(0, -3).Value
ws1.Range("B6").Value = ActiveCell.Offset(0, -4).Value
sPDFName = ActiveCell.Value
sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
'Check if worksheet is empty and exit if so
If IsEmpty(ws.UsedRange) Then Exit Sub
Set pdfjob = New PDFCreator.clsPDFCreator
'Check if PDFCreator is already running and attempt to kill the process if so
Do
bRestart = False
Set pdfjob = New PDFCreator.clsPDFCreator
If pdfjob.cStart("/NoProcessingAtStartup") = False Then
'PDF Creator is already running. Kill the existing process
Shell "taskkill /f /im PDFCreator.exe", vbHide
DoEvents
Set pdfjob = Nothing
bRestart = True
End If
Loop Until bRestart = False
'Assign settings for PDF job
With pdfjob
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Delete the PDF if it already exists
If Dir(sPDFPath & sPDFName) = sPDFName Then Kill (sPDFPath & sPDFName)
'Print the document to PDF
ws3.PrintOut copies:=1, ActivePrinter:="PDFCreator"
'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
ActiveCell.Offset(1, 0).Activate
If Not IsEmpty(ActiveCell.Value) Then
ws1.Range("B20").Value = ActiveCell.Value
ws1.Range("B25").Value = ActiveCell.Offset(0, -2).Value
ws1.Range("B7").Value = ActiveCell.Offset(0, -3).Value
ws1.Range("B6").Value = ActiveCell.Offset(0, -4).Value
End If
End If
Next
End sub