We are trying to automate the creation of PDF files for each sheet in an Excel file,a nd keep getting error messages. Can someone help? It produces the post script file, but it has no data nd then doesn't create a PDf file at all. We have Excel 2003, and have the below code setup.
We also have a class module setup called cAcroDist with the below code.Set appDist = New cAcroDist Sheets("CAD Roll").Select InputPSFileName = "\\nanflp03\cola\123g\coladmin\Dreamweaver\New Albany CMA\Reports\Today\RollCAD.ps" OutPutPDFFileName = "\\nanflp03\cola\123g\coladmin\Dreamweaver\New Albany CMA\Reports\Today\RollCAD.pdf" Application.ActivePrinter = "Acrobat PDFWriter on LPT1:" ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _ "Acrobat PDFWriter on LPT1:", PrintToFile:=True, Collate:=True, PrToFileName:="\\nanflp03\cola\123g\coladmin\Dreamweaver\New Albany CMA\Reports\Today\RollCAD.ps" Call appDist.odist.FileToPDF(InputPSFileName, OutPutPDFFileName, sjobOptions) Kill InputPSFileName
Option Explicit Public WithEvents odist As PdfDistiller Private Sub Class_Initialize() Set odist = New PdfDistiller End Sub
Last edited by mkern21; 06-25-2009 at 09:10 AM. Reason: edit code
Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
You do have Acrobat and not just the reader?
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Sorry about the formatting of the code don't use these very often.
But, yes i do have the full version of acrobat.
You need to add a Class Module & name it - cACroDist. Paste this code
In a Standard Module:Option Explicit Public WithEvents odist As PdfDistiller Private Sub Class_Initialize() Set odist = New PdfDistiller End Sub
Finally you need to set a Reference to Acrobat Distiller in the VB EditorSub PrintWholeWorksheetToPDF() 'change file name & path to match your needs Dim sPSFileName As String 'Name of PS to be created Dim sPDFFileName As String 'Name of PDF to be created Dim sJobOptions As String Dim sCurrentPrinter As String 'Same current printer choice to resume at end Const sPDFVersionAndPort As String = "Adobe PDF on Ne07:" Dim appDist As cAcroDist On Error GoTo exit_handler Set appDist = New cAcroDist sCurrentPrinter = Application.ActivePrinter 'Save the currently active printer sPSFileName = ThisWorkbook.Path & "\MyFileName.ps" 'Name of PS file sPDFFileName = ThisWorkbook.Path & "\MyFileName" & ".pdf" 'Name of PDF ThisWorkbook.Sheets.PrintOut ActivePrinter:=sPDFVersionAndPort, _ PrintToFile:=True, PrToFileName:=sPSFileName 'Prints to PS 'Creates PDF Call appDist.odist.FileToPDF(sPSFileName, sPDFFileName, sJobOptions) Kill sPSFileName 'Removes PS Application.ActivePrinter = sCurrentPrinter 'Change back to the original printer MsgBox "Done", vbInformation, "PDF Created" Exit Sub exit_handler: MsgBox "Could not create PDF", vbCritical, "Error occurred" End Sub
Tools -. References
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
That is what I have, I believe...as you can see above I did include the cAcroDist class module, and I believe my code is the same except for the error handling (which i didn't include), and the dim statements, which i do have I just didn't copy them above. This is exactly what I have entirely in my code.
And My CAcroDist class module is as followsPublic Sub CommandButton2_Click() Dim InputPSFileName As String Dim OutPutPDFFileName As String Dim sJobOptions As String Dim appDist As cAcroDist Msg = "Are you sure you want to PDF the SkipCADRoll Reports?" Style = vbYesNo + vbQuestion Title = "Create PDFs?" Response = MsgBox(Msg, Style, Title) If Response = vbYes Then Set appDist = New cAcroDist Sheets("CAD Roll").Select InputPSFileName = "\\nanflp03\cola\123g\coladmin\Dreamweaver\New Albany CMA\Reports\Today\RollCAD.ps" OutPutPDFFileName = "\\nanflp03\cola\123g\coladmin\Dreamweaver\New Albany CMA\Reports\Today\RollCAD.pdf" Application.ActivePrinter = "Acrobat PDFWriter on LPT1:" ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _ "Acrobat PDFWriter on LPT1:", PrintToFile:=True, Collate:=True, PrToFileName:="\\nanflp03\cola\123g\coladmin\Dreamweaver\New Albany CMA\Reports\Today\RollCAD.ps" Call appDist.odist.FileToPDF(InputPSFileName, OutPutPDFFileName, sJobOptions) Kill InputPSFileName Msg = "All Acrobat Drop Files have been created successfully." Style = vbOKOnly + vbInformation Title = "PDF Files Successful" Response = MsgBox(Msg, Style, Title) Cells(15, 3).Value = DateInsert Application.CutCopyMode = False Else Msg = "The report will not be PDFed." Style = vbOKOnly + vbExclamation Title = "PDF Cancelled" Response = MsgBox(Msg, Style, Title) Cells(10, 8).Value = "Aborted " + DateInsert Application.CutCopyMode = False End If End Sub
I did already have the Acrobat Distiller reference selected, but it still will not work....I just re-ran it and it looks like it actually created the PDF file in my y:\ drive (which is our personal drive) instead of the location specified in the OutPutPDFFileName in the code. What would cause this? is it a default setting perhaps on my Acrobat?Option Explicit Public WithEvents odist As PdfDistiller Private Sub Class_Initialize() Set odist = New PdfDistiller End Sub
Last edited by mkern21; 06-25-2009 at 08:07 AM. Reason: code tags not working
Please use Code tags.
Have you tried my code, it certainly works for me.
You must make the Reference to Acrobat Distiller as i said in my previous post
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
I did set the reference to Acrobat Distiller, and it appears different people are getting different results. I will try your exact code in another file and see if I get a different result, but it looks the same.
When I run it it creates a pdf file in my y:\drive when it does executes the printout line of code and then does nothing on the call apdist....function, and my co-worker gets the following error on the call apdist line
Method 'FiletoPDF' of object 'IPDFDistiller' failed
ok your code did work so it must just be the way we have it setup....can you see anything in our code that would cause the issue?
maybe the printer name is incorrect in your code.
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
I got it to work i think there was something wrong in our printout statement or something, because I just copied what you had and it worked finally!! Thanks!!
Just found this code and before I continue thanks for a great bit of code, we are using the code but have discovered a problem which we can't get our heads around! I am using Excel 2007 and have used the attached code, my problem is that when I run it the code goes into my excel drop down box, selects the first name then pdf's the data, however when it loops around to the next item on the list it generates the .ps file but no pdf is created, it then moves on to the 3rd drop down name and correctly pdf's it!! Really odd as it basically pdf's every other name in the drop down but annoyingly it select every item but only generates every other one!
Any help would be gugely appreciated as it is taking twice as long to run;
Sub ChangeMASelection()
Dim Rng As Range
Dim c As Range
Dim InputPSFileName As String
Dim OutPutPDFFileName As String
Dim sJobOptions As String
Dim appDist As cACroDist
Dim StrAgent, StrPath As String
' Change reference to cell containing validation to suit
With Range("DropDownList")
Set Rng = Range(Mid(.Validation.Formula1, 2, 255))
For Each c In Rng
.Value = c.Value
StrAgent = Range("DropDownList") 'get the managing agent name
StrPath = ThisWorkbook.Path
Set appDist = New cACroDist
Sheets("Charts").Select
InputPSFileName = "\\lnscntfs02\fpddata\Claims\Analysis Services Regular Report Production Files\ECF\ECF_Dashboard_Final.ps"
OutPutPDFFileName = StrPath & "\" & StrAgent & "_ECF Dashboard Report.pdf"
'OutPutPDFFileName = "\\lnscntfs02\fpddata\Claims\Analysis Services Regular Report Outputs\ECF_CTP Dashboards\2011 ECF\2011 02\ECF Dashboards\" & StrAgent & "_ECF Dashboard Report.pdf"
Application.ActivePrinter = "Adobe PDF on Ne02:"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
"Adobe PDF on Ne02:", PrintToFile:=True, Collate:=True, PrToFileName:="\\lnscntfs02\fpddata\Claims\Analysis Services Regular Report Production Files\ECF\ECF_Dashboard_Final.ps"
Application.Wait Now + TimeValue("00:00:55")
Call appDist.odist.FileToPDF(InputPSFileName, OutPutPDFFileName, sJobOptions)
On Error Resume Next
' Application.Wait Now + TimeValue("00:00:05")
Kill InputPSFileName
Next c
End With
End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks