Click here to Skip to main content
15,918,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does anybody know how to program the "Microsoft Print to PDF" virtual printer downloaded with Windows 10.
I want to print an Excel worksheet using "Microsoft Print to PDF" virtual printer.
I want to be able to set the path and filename of the pdf file within the code.
I have tried the code below but it requires the user to put the filename and path in before creating the PDF file.

What I have tried:

VB
Private Sub SaveWorksheetAsPDF(Title As String, Path As String)
        Dim FileName As String = "ResultsSheet" & Title & ".pdf"

        'Print the document to PDF
        oSheet.PrintOut(copies:=1, Preview:=True, ActivePrinter:="Microsoft Print To PDF")

       'Wait until the file shows up before closing PDF Creator
        Do
            My.Application.DoEvents()
        Loop Until Dir(Path & FileName) = FileName

     End Sub
Posted
Updated 22-Feb-16 8:49am
Comments
Maciej Los 22-Feb-16 14:46pm    
I'm affraid you can't define the name of file you want to create via pdf printer. You can only print the copy of sheet to virtual pdf printer.

1 solution

You need to pass the PrToFileName and PrintToFile parameters to the PrintOut method[^]:
VB.NET
Dim fullPath As String = System.IO.Path.GetFullPath(FileName)
oSheet.PrintOut(copies:=1, Preview:=True, ActivePrinter:="Microsoft Print To PDF", PrToFileName:=fullPath, PrintToFile:=True)
 
Share this answer
 
Comments
Dave the Golfer 22-Feb-16 17:32pm    
Richard

Thanks for the advice. All is working OK.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900