0

I'm updating old VB6 code to save its DataReports out to a PDF, rather than bringing up a print dialog.

I cannot simply write the PDF within the code (using a VB6 PDF library, etc.), since all our software already uses DataReports, and writing print code for each one would be tedious, at best. Currently, the process requires an employee to print the DataReport to a PDF print driver, naming it manually and saving it to where it needs to go. I need to automate this all, so that the name and location of the saved PDF can be specified programatically, rather than entered by hand.

The best solution would be if DataReport simply had a .SaveToPdf(filename) routine. Worst-case scenario, I see myself automating the process using SendKeys. However, this solution needs to work in an environment with multiple possible printers (so the PDF print driver might not be the default,) and on Windows XP, Vista, or 7.

I've fruitlessly tried Googling the problem, which returns only tutorials on how to do it by hand, much as we do now.

dlras2
  • 8,416
  • 7
  • 51
  • 90

3 Answers3

2

You might consider using a PDF Printer Driver that allows you to configure silent "printing" to a preset directory using auto-generated names.

For an example of such a product, see:

http://www.iteksoft.com/modules.php?op=modload&name=Sections&file=index&req=viewarticle&artid=21

Bob77
  • 13,167
  • 1
  • 29
  • 37
  • This is the best solution I've found. eDocPrinter PDF Pro (the software you linked,) even allows you to set all its settings through the registry, so you can specify names and directories, etc. Adobe PDF Printer allows for a silent mode, but I did not see any way to programatically set its settings. – dlras2 Jun 08 '11 at 04:42
1

I would create a dialog that lets the user enter the printer (driver) name, directory to save to, and a file naming guideline, then save that to either a local ini file or the registry. You would then need two print buttons / menus. One to print straight to the printer using the default (saved) settings, and one that opens the print window they see now so they can perform a custom print.

Remember an ellipsis on a menu item indicates additional dialogs, Print vs Print...

jac
  • 9,666
  • 2
  • 34
  • 63
  • This is what I would do, but doesn't give any information on how to automate the process. I'm still calling `DataReport1.PrintReport` and how do I even specify the PDF's name? You don't actually save anything until the PDF print driver generates the output, then asks you where to save it. Unless you have suggestions for an programatic PDF print driver, which might work. – dlras2 Jun 06 '11 at 17:17
  • 1
    I guess I misread exactly what you were looking for. If you're after a code example of how to send your report to a pdf printer driver you should add what you're using to do your printing. Here is a post by @Maciej that demonstrates printing using GhostScript, http://stackoverflow.com/questions/2599925/how-to-print-pdf-on-default-network-printer-using-ghostscript-gswin32c-exe-shel – jac Jun 06 '11 at 17:37
  • GhostScript would work well, if I could print a DataReport to a PostScript file. All I know how to do is print via a print dialog, and export as HTML or text, neither of which keep the same formatting as printing. I didn't specify what I'm using to do my printing because I'm willing to use anything that will work. – dlras2 Jun 06 '11 at 17:49
  • @Cyclotis04 So are you using VB to call something like Crystal Reports for the DataReport? – jac Jun 06 '11 at 17:55
  • I am using a `MSDataReportLib.DataReport`, which I design in design time, populate in runtime from an mdb, then call `DataReport1.PrintReport`. `PrintReport` can print silently to the default printer by setting `ShowDialog` to false, but this still brings up the PDF print driver dialogue (assuming it was my default printer - which it may not be,) asking what I'd like to name the file and where to save it. – dlras2 Jun 06 '11 at 18:00
0

Just use Crystal Report Viewer Control and follow the steps:

Set objRpt = objApp.OpenReport("type report path and name")

objRpt.DiscardSavedData
dim filepath as string
filepath = report path & report filename

With objRpt

    .ExportOptions.FormatType = crEFTPortableDocFormat
    .ExportOptions.DestinationType = crEDTDiskFile
    .ExportOptions.DiskFileName = 'filepath string goes here
    .ExportOptions.PDFExportAllPages = True
    .Export False
End With

Follow these steps and the export is done.

Luís Cruz
  • 14,780
  • 16
  • 68
  • 100