Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
When i put the print button code in my vb form:
VB
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = True
        PrintForm1.Print()

it gives me the error: Object variable or With block variable not set
for line :PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = True
Posted
Updated 21-Feb-11 14:58pm
v2

1 solution

Possible reason:

- PrintForm1 could be null
- PrintForm1.PrinterSettings could be null
- PrintForm1.PrinterSettings.DefaultPageSettings could be null.

Please check for null before you proceed.

Update 1:

Null Check in VB:

VB
If PrintForm1 IsNot Nothing Then

'YOUR CODE

End If


Nish's Guess:

PrintForm1 is null.

Try,

VB
PrintForm1 = New PrintForm1
PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = True
PrintForm1.Print()


Mark it as answer if it is helpful
 
Share this answer
 
v3
Comments
waqi19 21-Feb-11 21:27pm    
how u check it?
waqi19 21-Feb-11 21:27pm    
coz my teacher didnt teach us this and he just put the whole code for us to copy.
Nish Nishant 21-Feb-11 21:28pm    
You need to instantiate PrintForm1.
Nish Nishant 21-Feb-11 21:28pm    
Voted 5, good answer.

Most likely null candidate is PrintForm1.
Venkatesh Mookkan 21-Feb-11 21:35pm    
Thanks Nish. I have added your comment to the answer.

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