Hello, it is possible to use the Printer object to save PDF files with the Microsoft Print to PDF driver and without user interaction.
HTH
Eduardo - this code doesn't seem to work for me on Win10? i just get printer error 482? i have this Win10 machine configured with only printer installed is "Microsoft Print to PDF". Should your code be running as-is?
Eduardo - this code doesn't seem to work for me on Win10? i just get printer error 482? i have this Win10 machine configured with only printer installed is "Microsoft Print to PDF". Should your code be running as-is?
Change the path "D:\test.pdf" to whatever is valid on your system.
Hello, it is possible to use the Printer object to save PDF files with the Microsoft Print to PDF driver and without user interaction.
HTH
What is the purpose or principle of mReplaceAPI?
Print to specific PDF file name
Assumes Windows 10 with the optional Microsoft Print to PDF driver installed. This is normally the default unless the user has removed it via Windows Features.
Bare bones example. Note that for the most part printing must be done via API calls to avoid raising the driver's file picker dialog
Yes, you are not allowed to write to the root C:\
Make a folder and write to it, like C:\New folder\test.pdf
Yes, that was all that the problem was. thanks, Edwardo. your program works great.
Add'l question....
is there a way to autofill the Windows dialog the "Microsoft Print to PDF" presents to the user, and allow the dialog to remain operational? One of the issues that users have is that the windows dialog initially shows with no filename, and users get error if they click either 'Save' or 'Cancel' without properly entering a valid filename. Yes, the users should pay closer attention to their entry and random clicking, but a preventative solution would be to autofill a default FileName, while still allowing the user to use the windows dialog to change directory selection or change the filename if desired.
Yes, that was all that the problem was. thanks, Edwardo. your program works great.
Add'l question....
is there a way to autofill the Windows dialog the "Microsoft Print to PDF" presents to the user, and allow the dialog to remain operational? One of the issues that users have is that the windows dialog initially shows with no filename, and users get error if they click either 'Save' or 'Cancel' without properly entering a valid filename. Yes, the users should pay closer attention to their entry and random clicking, but a preventative solution would be to autofill a default FileName, while still allowing the user to use the windows dialog to change directory selection or change the filename if desired.
I think you can't, because if you set the name, then it doesn't ask.
You will have to add your own common dialog Save As to do that.
Hello, it is possible to use the Printer object to save PDF files with the Microsoft Print to PDF driver and without user interaction.
HTH
Works perfectly. Extremely simple to use. I like it! Does it store the default printer and set it back? (I have mine set to Microsoft PDF already so I didn't check). Thanks!
Does it store the default printer and set it back?
No, but easy to add that:
Code:
Private Sub Command1_Click()
Dim strCurrentPrn As String
strCurrentPrn = Printer.DeviceName
If Not SelectPrinter("Microsoft Print to PDF") Then
MsgBox "Can't save PDF, 'Microsoft Print to PDF' driver not present", vbCritical
Exit Sub
End If
PrinterFilePath = "D:\test.pdf"
Printer.Print "AAA"
Printer.FontSize = 34
Printer.Print "BBB"
Printer.DrawWidth = 10
Printer.Circle (1000, 1000), 500, vbRed
Printer.EndDoc
PrinterFilePath = ""
SelectPrinter strCurrentPrn
End Sub
Could I ask what the difference is between your work and this one vbImg2Pdf
I don't think the two projects have much in common.
vbimg2pdf:
Convert jpeg/png images to multi-page pdf file
You can do that also with this project, but with this project you can print anything, not just images.
And the most important feature is that you do it with the Printer object.
On the other and, it seems that that project splits automatically the images into several PDF pages. I don't think the Printer object will do that automatically.
So, basically, they are two completely different projects, with different goals.
Also, another key difference is that this method requires the "Microsoft Print to PDF" driver installed.
Last edited by Eduardo-; Oct 31st, 2021 at 10:14 AM.
Hi, someone can guide me to use this apis in order to save a datareport to PDF, using a pdf printer (Microsoft print ot pdf)?
i tried everything without success :-(
Thanks in advance
If you could post some example project where you say you experience the problem maybe we could investigate what is happening and look for a solution. (PS: a simple project)
I found "why it wasn't working", i used your code from the first post, but it never filled the pdf file, and today... looking quietly my code, i found that i was setting the "microsoft printer to pdf " with a loop and "set printer = x", BUT not with the API that sets the default printer.
Once i selected the Microsoft Printer to PDF as "default", not only with the set printer... it worked like a charm.
Just tested:
I downloaded the sample project from the first post.
Compiled the exe.
Ran it and clicked the button.
The file d:\test.pdf was created as expected.
Windows 11 22H2 with up to date automatic updates installed.
My project runs fine in the IDE and produces a perfect PDF report.
However when I try to compile I get the following error.
---------------------------
Microsoft Visual Basic
---------------------------
Programmatic ID string too long 'TracsDashboard.cAutoRestoreFunctionOnExit'. The Programmatic ID must be 39 characters or less.
---------------------------
OK Help
---------------------------
My project runs fine in the IDE and produces a perfect PDF report.
However when I try to compile I get the following error.
---------------------------
Microsoft Visual Basic
---------------------------
Programmatic ID string too long 'TracsDashboard.cAutoRestoreFunctionOnExit'. The Programmatic ID must be 39 characters or less.
---------------------------
OK Help
---------------------------
The project name and a Class name cannot exceed 39 characters as the error message implies.
So for your project named TracsDashboard then change cAutoRestoreFunctionOnExit class name to something shorter.
The project name and a Class name cannot exceed 39 characters as the error message implies.
So for your project named TracsDashboard then change cAutoRestoreFunctionOnExit class name to something shorter.
Thanks that did it!
I understood what the message implied but I wasn't sure if a declaration could be changed instead of changing names.
Checked, they are fine, i will start a new project adding slowly all the .bas files.
I think the new vbflexgrid from Krool it's making some kind of problem, i deleted (in a copy) the uctabmdi files, the sendmail and some other with apis ...
i checked that no public apis exists inside the project...
Weird.. i have to spend more time finding where this problem is :_(
Just copy the files mReplaceAPI.bas and AutoRestoreFunctionOnExit.cls as they are here and it should work fine, since the APIs are all there and are declared Private.
If it still crashes then it must be something else.
I used the code to save a datareport to a pdf from a Mdi form. Everything was ok.
But i had to change the form from Mdi child to one normal, but... modal...
And.. i was still launching the datareport whitout being modal .... , (i can launch a normal form / datareport from a modal form, without doing it modal too) the strange thing is no error was sent anywhere... but i found it finally.. working like a charm again!
Just copy the files mReplaceAPI.bas and AutoRestoreFunctionOnExit.cls as they are here and it should work fine, since the APIs are all there and are declared Private.
If it still crashes then it must be something else.
Why do you create some amazing projects every time? I suspect that you have worked in Microsoft for several months.
As the title says, this code is for saving PDF files with "VB's Printer object".
DataReport <> Printer object.
Anyway the code should work also transparently for any other component that used the StartDocA API.
But, you shouldn't mix DataReport commands with Printer.EndDoc, they are different objects/components.
For working with other components, you need to ensure that before starting a print job, the component will use the "Microsoft Print to PDF" driver for printing.
That's done for the Printer object in the sample code in the SelectPrinter procedure, but that will only select the current printer for the Printer object, not for other components.
I don't remember how to select the current printer in DataReport (look on internet, or in this forum, or ask in the general forum, or maybe someone seeing post it here).
And at the end, let the component (DataReport in this case) do the "EndDoc" for itself, don't call Printer.EndDoc because it has nothing to do.
Hi! Did you really get this working? I mean, it works for me, however when I open the PDF file, I get: Acrobat reader could not open test.pdf because it is either not a supported file or because the file has been damaged (for example, it was sent as an email attachment and wast correctly decoded)
Hi! Did you really get this working? I mean, it works for me, however when I open the PDF file, I get: Acrobat reader could not open test.pdf because it is either not a supported file or because the file has been damaged (for example, it was sent as an email attachment and wast correctly decoded)
Any clues, please?
You need to set "Microsoft Print to PDF" as the printer.
I don't know how to do that with code because I never used DataReports.
And I don't think that the Printer.EndDoc line is necessary for DataReports.
You need to set "Microsoft Print to PDF" as the printer.
I don't know how to do that with code because I never used DataReports.
And I don't think that the Printer.EndDoc line is necessary for DataReports.
Hey! Thank you for responding. I did have it set to "Microsoft Print to PDF", and somehow, 40 minutes ago, I changed the position of the DataReport.Refresh before FilePath, and for some reason, it worked... go figure!!!
Like another user said, without P-CODE, the app and sometimes VB6 as I was testing, would just close unexpectedly. I wish I knew what is causing that, because I never used P-CODE (don't even know what it is) and I am afraid that using PCODE, will create other issues... But I honestly have no idea what to look for, I am not that good with VB6.
By the way, amazing code, it really solved my immediate problem, as I could not for the life of me, find Crystal Reports to even try doing the report I needed. Unfortunately, I cant code the report using your example, but I will test it a little more and hopefully it wont be an issue using PCODE.
Really great code! Are you Brazilian? They used to code VB6 like no one else.
Thank you again, and if you ever, ever figure out what makes the code not work without P-CODE (or if it does not matter), please let me know!