• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 795
  • Last Modified:

Printer Object in VB6 prints an extra blank page

Hi all,
Ok I’ve given up on the printer object and since there is no shortage of complaints about it I said to myself; what’s another question among the 100’s? So here goes:

I am trying to create a very simple address label using some very basic code. For some reason whenever I test the code the printer always prints an extra blank page.

I have tried to set the scaleMode to Twips, inches, millmeters etc… nothing worked.

I’ve also tried setting the scaleHeight and scaleWidth and still nothing that prevents the blank paper from coming out. Even when I set the two mentioned properties to only 1!
I think it is worth mentioning that I am printing over a network printer.

Any Ideas?
Regards

Here is the code which I am using:

Option Explicit
 Dim PRN As Object
Private Sub Form_Load()
   
   Dim YPos As String
    Dim str As String
    Dim fnt As New StdFont
   
    str = "test"
    fnt.Name = "Verdana": fnt.Size = 11: fnt.Bold = True
    CommonDialog1.CancelError = True
   
    Set PRN = Printer
    With PRN
        .PaperSize = vbPRPSLetter
        .ScaleWidth = 11222
        .ScaleHeight = 11904
        .CurrentX = 30
        .CurrentY = 1
        .CurrentX = 0
        .CurrentY = PRN.CurrentY + 5
    End With
       
    YPos = PRN.CurrentY
   
    On Error Resume Next
    CommonDialog1.ShowPrinter
    If Err.Number = 32755 Then
        Exit Sub
    End If
   
    On Error GoTo 0
    If CommonDialog1.Orientation = cdlLandscape Then
        Printer.Orientation = cdlLandscape
    End If
    ClearPrintoutArea
   
    Set PRN.Font = fnt
        PRN.Print str
        Printer.EndDoc
End Sub

Private Sub ClearPrintoutArea()
    If PRN Is Printer Then
        PRN.NewPage
    Else
        PRN.Cls
    End If
End Sub



0
gbouch2
Asked:
gbouch2
  • 10
  • 8
  • 6
  • +1
1 Solution
 
LunaSkyeCommented:
Ok,
well i see that the problem is in the "ClearPrintoutArea" procedure.

the NewPage function ADVANCES to the NEXT PAGE. YOu are already on teh first page of printing by default. If you go .NewPage.. you are accepting the first page as is.. (Blank) and moving on to the second one..

You dont have to clear the page to start printing.. just start printing..  Ther eare no printing commands used prior to the call to "ClearPrintoutArea".. so just dont call it.


Also, I would put the dialog choice first.. and use CommonDialog1.TrackChanges = True.. This way the changes made in the dialog box will affect the PRINTER object in VB.

- Andrew
0
 
Leo EikelmanCommented:
hmm I don't see in your code if you explicitilly set your page size to A4 and to landscape.

This is probably an issue with either your page or margin size being too large.

If you did not, set your page size to A4 and orientation to landscape.

hope that helps,

Leo
0
 
LunaSkyeCommented:
I mean

Printer.TrackDefault = True

This will change the PRINTER OBJECT in VB...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LunaSkyeCommented:
No, he sets his page size here:


 With PRN
        .PaperSize = vbPRPSLetter



The only weird part is that he then CHANGES the scale size ... The scale size is set to default values after you set paper size to LETTER.  I dont ever mess with those values..

- Andrew
0
 
LunaSkyeCommented:
Also,
If you want landscape, you should be setting the printer property:

Printer.Orientation = prORLandscape

This too will change the default values of the scale width, etc..

There is no need to set these values manually.. YOu cannot FORCE a page to turn on its side by making its scale width and height the same as a landscape print woudl be..

Rather, you should change teh ORIENTATION and PAPERSIZE settings.. and THEN the scale size settings will cahnge as they should.

Howqever, if you use "TRACKDEFAULT" as i said earlier, then if you choose "Landscape" on the dialog box.. that choice will be reflected in teh printer object.

- andrew
0
 
Leo EikelmanCommented:
Yeah I don't think it's the first issue you mentioned with the "ClearPrintoutArea" procedure.  It's definately coming from the size of the page.  You are changing the size of the page and orientation instead of just setting the built in orientation and papersize settings.

Cheers,

Leo
0
 
g_johnsonCommented:
The call to clearprintoutarea is doing this with it's newpage method.

I always write such routines to include a parameter intPage (page number) and then only call the newpage method if intPage > 1 (I use these routines to print headings, etc.)

0
 
gbouch2Author Commented:
LunaSkye, leikelman and g_johnson

Thanks for all your inputs, i really appreciate it, however I have tried all the suggestions so far and with all due regret nothing has worked

I even disable almost all properties and the print procedure looks like this:

Set PRN = Printer
    With PRN
        .PaperSize = vbPRPSLetter
        .CurrentX = 30
        .CurrentY = 1
        .CurrentX = 0
        .CurrentY = PRN.CurrentY + 5
    End With
       
    YPos = PRN.CurrentY
       Set PRN.Font = fnt
        PRN.Print str
        Printer.EndDoc

(Notice that there is no longer a call the "ClearPrintoutArea")
Even when the dialog box appears and even if I explicitly select the A4 size from the preferences still the same result!
Any other ideas please?

Regards
0
 
Leo EikelmanCommented:
What if u get rid of the lines

------------------------------------
.CurrentX = 30
.CurrentY = 1
.CurrentX = 0
.CurrentY = PRN.CurrentY + 5

---------------------------------------

what happens when you try to print without setting any manual page properties?

Leo




0
 
LunaSkyeCommented:
Have you guys run it yourselves??
Yes, the adjusting the size manually is not good practice, but it doesnt make this problem happen.

The problem is with the "NewPage" function in the "ClearPrintoutArea" function.. As far as I see, its that simple.  Dont call NEWPAGE unless you want to advance to the NEXT PAGE..

- ANdrew
0
 
gbouch2Author Commented:
Hum Believe it or not, it actually solved the problem! Weird! I don't see how setting the coordinates of the text to be printed can cause a whole new blank page to print.Especially that the values measure only few twips!  What concerns me now is how I am going to set the positioning of the text on the label if I cannot use these properties. But i guess I’ll leave that for another thread. :-)

Thanks Leo, I wish I can award all of you the points. Alas I have only one choice.

Thanes Guys
0
 
gbouch2Author Commented:
Oups sorry I meant the points go to leikelman
0
 
LunaSkyeCommented:
Ok,
GBouch2.. could you put all the code in??
SOme is missing.

Put everything..
Including the seutp of the font, the assigment of the TestString... All of it..

Thank yuou,,
 andrew
0
 
gbouch2Author Commented:
LunaSkye the call to ClearPrintoutArea was already disabled but did not actually solve the problem.
0
 
gbouch2Author Commented:
As requested by LunaSkye here is the complete code, which is now working. Notice the disabled properties

Option Explicit
 Dim PRN As Object
Private Sub Form_Load()
   
   Dim YPos As String
    Dim str As String
    Dim fnt As New StdFont
   
    str = "test"
    fnt.Name = "Verdana": fnt.Size = 11: fnt.Bold = True
    CommonDialog1.CancelError = True
   
    On Error Resume Next
   
    CommonDialog1.ShowPrinter
    Printer.TrackDefault = True
    If Err.Number = 32755 Then
        Exit Sub
    End If
   
    On Error GoTo 0
    'If CommonDialog1.Orientation = cdlLandscape Then
     '   Printer.Orientation = cdlLandscape
    'End If
   
    Set PRN = Printer
    With PRN
        .PaperSize = vbPRPSLetter
        '.ScaleWidth = 1
        '.ScaleHeight = 1
        '.CurrentX = 30
        '.CurrentY = 1
        '.CurrentX = 0
        '.CurrentY = PRN.CurrentY + 5
    End With
       
    YPos = PRN.CurrentY
   
   
        'ClearPrintoutArea
   
    Set PRN.Font = fnt
        PRN.Print str
        Printer.EndDoc
        End
End Sub

Private Sub ClearPrintoutArea()
    If PRN Is Printer Then
        PRN.NewPage
    Else
        PRN.Cls
    End If
End Sub


0
 
LunaSkyeCommented:
Yeah sorry,

I got "Type-Happy" before i read everythign ... :-) hehe

Im glad your problem is fixed.

PEACE!!

- Andrew
0
 
Leo EikelmanCommented:
It is definately setting the page properties manually.  As already said, it has nothing to do with the ClearPrintoutArea procedure.

Cheers

Leo
0
 
LunaSkyeCommented:
Yeah, but you wanna know the strange part??
I copied and pasted your code EXACTLY as it was in the first example.. removed the NEWPAGE thing.. and it worked..
Hmm.. go figure.

- Andrew
CHEERS!!!
0
 
Leo EikelmanCommented:
That could be because your printer supports the parameters he was setting to fit on one page and gbouche2's printer did not.
0
 
gbouch2Author Commented:
It is surprising you know!

I just recopied the same code pasted above, enabled the two line:

.CurrentX = 30
.CurrentY = 1

And yes you guessed it, two pages came out (Notice the call to new page was still disabled)...
I don't get it!
0
 
LunaSkyeCommented:
what is scalemode of the printer?? Is it twips? or User? or pixels?

If you are printing text OUTSIDE of the boundaries of a page, it will force a new page..

If your scale is CRAZY-Out of whack.. you may just be sending the text out of bounds..
0
 
gbouch2Author Commented:
it is Twips
0
 
Leo EikelmanCommented:
in Twips, 1440 twips is 1 inch.  So maybe setting your y value to 1 was so small that for some ODD reason it was going to the next page.  I'm not sure how this could of happened, but apparently it did.

Leo
0
 
LunaSkyeCommented:

If you make your currentx = .. something like.. 200
and yoru currenty = 200.. (lets say)

THEN you print your text...

What happens?

0
 
gbouch2Author Commented:
LunaSkye,
I tried your latest suggestion and it actually printed ONE blank page

Leo
I already tried several Scale values; as a matter of fact I tried 8 then 7 then 6 all the way down to 1 each produced the same result which is printing two pages!

The only way it actually worked is by not setting any properties at all!
Maybe we should call it a divine intervention? LMAO

Cheers

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 8
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now