How to save (print) a report to pdf file by using code

anb001

Registered User.
Local time
Today, 03:40
Joined
Jul 5, 2004
Messages
197
Is it possible to save/print a report to a pdf file, by using just code?

I have a report which I would like to have saved/printed, by 'just' clicking a button. Preview of the report is not necessary. The report should be saved in a specific location on the computer, and if possible, I would also like it to be saved with a specific name, such as "Some text + [today's date]".

I have searched around for this a bit, but I haven't been successfull in finding what I would like. I would appreciate if someone can assist in this matter.

/Anders

P.s. Sorry if any dublicates of this post shows up. I had internet connection problems when sending, and had to refresh a few times.
 
The code is well documented. you copy and paste ALL in a Standard Module, give the module an other name than the function names ex. mdlPDFwriter

Code:
<!--StartFragment-->Public Function RunReportAsPDF(rptName As String, sPDFPath As String, sPDFName As String)
    '---------------------------------
    'rptName = Microsoft Access report name 
    '     you
    'want to create pdf from
    'sPDFPath = the directory path where you
    '     want
    'to create the pdf file (ex. - "c:\data\
    '     ")
    'sPDFName = the name of the pdf file you
    '     are
    'wanting to create (ex. - "file001.pdf")
    '     
    '---------------------------------

This explain how to use the code.

Code:
Private Sub SomeButton_Click()
Call RunReportAsPDF("MyReport", "c:\Folder\","Salesdata.pdf")
End Sub

But if you uses Access 2007/2010 you don't need this code as these versions support export as PDF.

JR
 
JR,

Thanks.

A couple of additional questions:

- It first told me to install a printer. That I solved by changing 'Pdfwriter' to 'Adobe Pdf', as mine is called. However, when running the code, then it prints to default printer. If I then select another printer as default, then adobe pdf is used, as the codes dictates. How can that be?

- It selects the correct report to print, but it doesn't start in the folder coded, and it doesn't give it the correct name. Any idea why?

- It is mentioned in the comments to the code, that additional code can be added in order to turn the pdf preview off, and then on again when finished. Any idea how to include that in the code??

I use Adobe Acrobat 9, and this should work in Access 2003.

/Anders
 
Last edited:
After reading som of the comments on the site, there are some issues with different PDF versions. However there are some other PDF coverters out there that dosen't relay on installing PDFPrinters, this one from Lebans is recomended by several others : http://www.lebans.com/reporttopdf.htm

Perhaps this is better suited for you.

JR
 
Seems like that is somewhat easier to implement :-)

I do have a question regarding the use. The first part of the function looks like this:

Code:
Public Function ConvertReportToPDF( _
Optional RptName As String = "", _
Optional SnapshotName As String = "", _
Optional OutputPDFname As String = "", _
Optional ShowSaveFileDialog As Boolean = False, _
Optional StartPDFViewer As Boolean = False, _
Optional CompressionLevel As Long = 0, _
Optional PasswordOpen As String = "", _
Optional PasswordOwner As String = "", _
Optional PasswordRestrictions As Long = 0, _
Optional PDFNoFontEmbedding As Long = 0, _
Optional PDFUnicodeFlags As Long = 0 _
) As Boolean

I would expect that I would have to insert/change values, if I want something else than default. E.g. if I want to specify a path/filename, then the specific line would look like this:

Code:
Optional OutputPDFname As String = "c:\Reports\Daily.pdf", _

But that doesn't work. It still saves in the default folder (My Documents). And it's the same for the rest of the lines.

Further down this is coded:

Code:
    If Len(OutputPDFname & vbNullString) = 0 Then
        sOutFile = Mid(strPathandFileName, 1, Len(strPathandFileName) - 3)
        sOutFile = sOutFile & "PDF"
    Else
        sOutFile = OutputPDFname
    End If

If I in stead of 'OutputPDFName' in this piece of code, write the path/filename, then it is being used.

I assmue I'm doing something wrong, and I would appreciate if someone can assist.

/Anders
 
Dont do this

Code:
Optional OutputPDFname As String = "c:\Reports\Daily.pdf", _

this information is sent when you call the function. Check out this demo fresh from the Spent Geezers Association. Dll files must be stored at same location as the mdb for the print to work. Thanks Lebans.
 

Attachments

I found that I just had to change the 'Call function' code, then it worked.

Everything is working as it should now.

/Anders
 

Users who are viewing this thread

Back
Top Bottom