VB.net Printform cant find path

842 Views Asked by At

So I am working on a form which need to be printed. I want to end up with a PDF file using Foxit PDF printer. The problem is that I cant figure out how to get the selected path as file location so I keep getting an the Path cannot be null. error. Where in the code should I put my filelocation when using the Printform? In this code the foldername is the location where I want to print.

Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click

    Dim folderDlg As New FolderBrowserDialog
    Dim foldername As String
    folderDlg.ShowNewFolderButton = True
    If (folderDlg.ShowDialog() = DialogResult.OK) Then
        foldername = folderDlg.SelectedPath
        Dim root As Environment.SpecialFolder = folderDlg.RootFolder

    End If

    PrintForm1.Print()

End Sub

Edit:

Actually deleted part of the code and still getting the same error (first part wasnt doing anything to start with I know that). All I am using now is:

Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click

    PrintForm1.Print()

End Sub

Also microsoft help database about Printform isnt helping since I have done exactly what it says and still getting the Path is Null error

Edit 2: So I am using this code now and it is working.

 Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click

    PrintDialog1.PrinterSettings = PrintForm1.PrinterSettings
    PrintDialog1.AllowSomePages = True
    If PrintDialog1.ShowDialog = DialogResult.OK Then PrintForm1.PrinterSettings = PrintDialog1.PrinterSettings

    With Me.PrintForm1
        .PrintAction = Printing.PrintAction.PrintToPreview

        Dim MyMargins As New Margins

        With MyMargins
            .Left = 10
            .Right = 10
            .Top = 10
            .Bottom = 10
        End With

        .PrinterSettings.DefaultPageSettings.Margins = MyMargins

        .Print()

    End With

End Sub

but as soon as I try to set which area it should print I get the following error: "Printing is not a member of powerpacks". I tried using the following code according to microsoft this is the way it should work.. I have no clue where the error comes from

.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)  
2

There are 2 best solutions below

4
On

You don't need a path to use printform. Printform just prints what you see on the screen to your default printer. You need to install "Visual Basic PowerPacks" to use this command. More explanation you may find here: https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/printing/how-to-print-a-form-by-using-the-printform-component

0
On

To preview your print you don't need to use printdialog and all this. You just click on printform1 in the designer,to get up the properties window of printform1. In printaction you choose PrintToPreview. Thats all its needed. These are all the lines I need:

Public Class Form1
Private Sub Exit_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Application.Exit()
End Sub

Private Sub Print_Click(sender As Object, e As EventArgs) Handles Button1.Click
    PrintForm1.Print()
End Sub

End Class