Code running in vbe page but not in workbook

rkotha

New Member
Joined
Feb 24, 2016
Messages
5
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
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top