Link to home
Start Free TrialLog in
Avatar of gbouch2
gbouch2Flag for Afghanistan

asked on

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



Avatar of LunaSkye
LunaSkye

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

Printer.TrackDefault = True

This will change the PRINTER OBJECT in VB...
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
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
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
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.)

Avatar of gbouch2

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Leo Eikelman
Leo Eikelman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of gbouch2

ASKER

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
Avatar of gbouch2

ASKER

Oups sorry I meant the points go to leikelman
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
Avatar of gbouch2

ASKER

LunaSkye the call to ClearPrintoutArea was already disabled but did not actually solve the problem.
Avatar of gbouch2

ASKER

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


Yeah sorry,

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

Im glad your problem is fixed.

PEACE!!

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

Cheers

Leo
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!!!
That could be because your printer supports the parameters he was setting to fit on one page and gbouche2's printer did not.
Avatar of gbouch2

ASKER

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!
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..
Avatar of gbouch2

ASKER

it is Twips
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

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

THEN you print your text...

What happens?

Avatar of gbouch2

ASKER

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