Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Using pdf995 in macro gets error

Posted on 2008-10-23
2
875 Views
Last Modified: 2012-08-13
I have a macro that selects and prints a chart.
When I do a "Record Macro" and tell it to print using PDF995, I get a good PDF file.
When I use :
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, PrToFileName:="c:\Zaas\dir1.pdf", the pdf file is three times bigger and won't open.
The error message says either the file's an incompatable format or it's corrupted.

I'd appreciate any help.
Thanks

Sub Dir1()
'
' Dir1 Macro
' Macro recorded 10/23/2008 by tmccar10
'
    ActiveWindow.Visible = False
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.ChartArea.Select
    Application.ActivePrinter = "PDF995 on Ne00:"
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, PrToFileName:="c:\Zaas\dir1.pdf"
    
End Sub

Open in new window

0
Comment
Question by:tmccar10
2 Comments
 
LVL 2

Accepted Solution

by:
TadSter earned 125 total points
ID: 22789707
Excel does not natively print to PDF but there are addins that can do the job. If you have Adobe Acrobat you should have "Adobe PDF" printer installed. You can print to the "Adobe PDF" printer and it will stop and ask the user for a filename. Not so good.
My preferred solution is to print to PS format then convert it to PDF using Ghostscript (free download) or Distiller. This method allows you to specify the file name in your code.
Install a printer driver that supports PS file format. Windows XP comes with drivers for "Apple Color LaserWriter 12/600" Add it as a printer and call it PDFwriter. It doesn't matter what port you use because you won't use the port; LPT1 is fine.
Download and install GhostScript (free) or Adobe Distiller. The code I have is for Distiller. Be sure that you have the references as noted in the code.

'found this code on the web; am not sure where anymore.
 
'paste this into a class module named cAcroDist
Option Explicit
 
Public WithEvents odist As PdfDistiller
Public blnFinished As Boolean
Dim StartTime As Date
 
Private Sub Class_Initialize()
    Set odist = New PdfDistiller
End Sub
 
Private Sub odist_OnJobDone(ByVal strInputPostScript As String, ByVal strOutputPDF As String)
    'Sheets("PDF_Output").Range("A65536").End(xlUp)(2) = strOutputPDF & " printed successfully at " & Now()
    blnFinished = True
    'Kill strInputPostScript
End Sub
 
Private Sub odist_OnJobFail(ByVal strInputPostScript As String, ByVal strOutputPDF As String)
    'Sheets("PDF_Output").Range("A65536").End(xlUp)(2) = strOutputPDF & " failed to print at " & Now() & vbCrLf
    blnFinished = True
End Sub
 
Private Sub odist_OnJobStart(ByVal strInputPostScript As String, ByVal strOutputPDF As String)
    StartTime = Now()
    'Sheets("PDF_Output").Range("A65536").End(xlUp)(2) = strOutputPDF & " is printing " & Now()
    blnFinished = False
End Sub
 
'paste this into a module
Option Explicit
'need type reference: Acrobat Distiller
 
Sub CreatePDF()
    Dim svOutputPDF As String
    Dim svInputPS As String
    Dim fileBakName As String
    Dim PrinterName
    
    svOutputPDF = "C:\Temp\File 1.PDF"
    svInputPS = "C:\Temp\File 1.PS"
    PrinterName = "PDFwriter"
 
    'print to PS
    ActiveWindow.SelectedSheets.PrintOut PrintToFile:=True, PrToFileName:=svInputPS, ActivePrinter:=PrinterName
 
    'converto to PDF
    Call MakePDFs(svInputPS, svOutputPDF)
End Sub
 
Sub MakePDFs(svInputPS, svOutputPDF) 'Will Riley - June 2004
    
     'Amended from code given at "I Get It!!!"
     'In order not to get an error essage from the Adobe Distiller
     'you MUST set up the following settings from the CC Exp Analysis
     'Worksheet
     'From the worksheet goto File>Print .. select the Distiller
     'Goto Properties & uncheck the tickbox under
     'Adobe PDF Settings entitled "Do Not Send Fonts to Distiller
     '
     '#############################################################
     '#The Code uses the following external reference libraries:  #
     '#Microsoft Office 10.0 Object Library (9.0 also works)      #
     '#Acrobat Distiller                                          #
     '#Go to Tools > References in the VB Editor and check them   #
     '#############################################################
     
    Dim appDist As cAcroDist 'see class module
    Dim svJobOptions As String
    Dim retval
    Dim x
    Dim fso
    
    Set appDist = New cAcroDist
        
        'We don't want to see the distiller window
    appDist.odist.bShowWindow = False
        'We are using Start/Done events. If we spool, they don't fire
    appDist.odist.bSpoolJobs = False
        'Excel can print to a PostScript file and name it, but it can't
        'print to a PDF file and name it. And we DON'T want to name each
        'file individually, thanks much.
        
        'This uses our distiller class mod to make the PDF file
        'Upon successful completion, it deletes the PostScript file
    Call appDist.odist.FileToPdf(svInputPS, svOutputPDF, svJobOptions)
        'convert PS to PDF (above)
        'Distiller is SLOW. We have to sit here until the JobDone Event
        'fires and changes blnFinished to true
    Do While Not appDist.blnFinished
        DoEvents
    Loop
 
Cleanup:
    Set appDist = Nothing
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.DeleteFile (Left(svOutputPDF, Len(svOutputPDF) - 3) & "log")
    fso.DeleteFile (svInputPS)
End Sub

Open in new window

0
 

Author Closing Comment

by:tmccar10
ID: 31509190
Thanks Tad-

Unfortunately, I need this to distribute this to a lot of people and my company doesn't provide Acrobat to many people.

I can use pdf995 with the same intervention  you mentioned. I was trying to avoid that, as you can imagine.

I appreciate the code and I'll keep it, just in case.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question