# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  Save PDF file with VBs Printer Object

## Eduardo-

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

----------


## wqweto

> 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


Yes, API hooking would the thermonuclear option here :-))

cheers,
</wqw>

----------


## Jimboat

> 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-

> 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.

----------


## Jimboat

> Change the path "D:\test.pdf" to whatever is valid on your system.


Yes, i changed to "C:\test.pdf".  does 'test.pdf' have to exist already?  maybe C:\ doesn't like write to?

----------


## Eduardo-

> does 'test.pdf' have to exist already?


No.




> maybe C:\ doesn't like write to?


Yes, you are not allowed to write to the root C:\
Make a folder and write to it, like C:\New folder\test.pdf

----------


## DaveDavis

> 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


 easy understand...

----------


## Eduardo-

> What is the purpose or principle of mReplaceAPI?


To hook the call to the StartDoc API to be able to set the file.

----------


## Jimboat

> 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.

----------


## Eduardo-

> 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.

----------


## Jimboat

> 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.


ok.  thanks again, Eduardo.
/jim

----------


## Darkbob

> 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!

----------


## Eduardo-

> Does it store the default printer and set it back?


No, but easy to add that:



```
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
```

----------


## DaveDavis

> To hook the call to the StartDoc API to be able to set the file.


Can this module be used as common function to hook other APIs? e.g. hook TextBox DrawTextA/W API so that we can print colorful text?

----------


## Eduardo-

> Can this module be used as common function to hook other APIs? e.g. hook TextBox DrawTextA/W API so that we can print colorful text?


Yes, I've hooked other APIs.

----------


## loquat

Could I ask what the difference is between your work and this one vbImg2Pdf

----------


## Eduardo-

> 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.

----------


## loquat

yes, have tested your code, very impressive


```
    Printer.NewPage
    Printer.PaintPicture LoadPicture("c:\1.jpg"), 0, 0
```

----------


## Calcu

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

----------


## Eduardo-

> 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)

----------


## Calcu

Hi, thanks for answering.

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.

Thanks a lot for your code !

----------


## vbrad

Me too! Just used this today and works great. Thanks Eduardo!

----------


## Crapahute

Your code is so clever! It is really useful to me.

Thank you

----------

