Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting printer orientation crashes my app... 1

Status
Not open for further replies.

OrthoDocSoft

Programmer
May 7, 2004
291
US
Folks,

This has been working for months, but now, when it gets to a particular line, an error occurs (error 842, "printer error"), and the app crashes I have isolated the line that crashes to this one:

Code:
Prn.Orientation = vbPRORPortrait

Here is the sub it is in:

Code:
Public Sub PrintPictureToFitPage(Prn As Printer, Pic As Picture, _
Optional ByVal strOrientation As String)

Const vbHiMetric As Integer = 8
Dim PicRatio As Double
Dim PrnWidth As Double
Dim PrnHeight As Double
Dim PrnRatio As Double
Dim PrnPicWidth As Double
Dim PrnPicHeight As Double
   
If strOrientation = "Landscape" Then

   Prn.Orientation = vbPRORLandscape   ' Wider than tall.
   
Else
   
   Prn.Orientation = vbPRORPortrait  ' Taller than wide.
   
End If

' Calculate device independent Width-to-Height ratio for picture.
PicRatio = Pic.Width / Pic.Height

' Calculate the dimentions of the printable area in HiMetric.
PrnWidth = Prn.ScaleX(Prn.ScaleWidth, Prn.ScaleMode, vbHiMetric)

PrnHeight = Prn.ScaleY(Prn.ScaleHeight, Prn.ScaleMode, vbHiMetric)

' Calculate device independent Width to Height ratio for printer.
PrnRatio = PrnWidth / PrnHeight

' Scale the output to the printable area.
If PicRatio >= PrnRatio Then
   ' Scale picture to fit full width of printable area.
   PrnPicWidth = Prn.ScaleX(PrnWidth, vbHiMetric, Prn.ScaleMode)
   PrnPicHeight = Prn.ScaleY(PrnWidth / PicRatio, vbHiMetric, _
      Prn.ScaleMode)
Else
   ' Scale picture to fit full height of printable area.
   PrnPicHeight = Prn.ScaleY(PrnHeight, vbHiMetric, Prn.ScaleMode)
   PrnPicWidth = Prn.ScaleX(PrnHeight * PicRatio, vbHiMetric, _
      Prn.ScaleMode)
End If

' Print the picture using the PaintPicture method.
Prn.PaintPicture Pic, 0, 0, PrnPicWidth, PrnPicHeight
   
End Sub

Can anyone see why this is causing problems?

Do you think it is because of the new printer being installed? (I doubt it...)

Actually, I never print in landscape, so I'm thinking about just removing the portrait/landscape bit altogether, but I hate "cheating" like that.

Also, I could write an error handler which cancels the print without crashing the app, and I can do that I think.

Thanks,

Ortho

[lookaround] "you cain't fix 'stupid'...
 
I find that Prn.Orientation = vbPRORLandscape will crash if prn represents the Microsoft Document Image Writer but can't remember any similar problem with Prn.Orientation = vbPRORPortrait. Have you tried it on another printer. Is the printer driver up to date etc.
 
Hugh,

Thanks. I will ask my IT people when they get back from holiday about the drivers.

My post was poorly constructed and I apologize. Here are some additional details.

Not all prints crash. Some print events work just fine.

As I always do when I have trouble like this, I put msgboxes in between each line of code to "tell" me what is going on at that step, if for no other reason than to detect which line of code is crashing my app. I started noticing that prnWidth and prnHeight were "some really long" values, and then noticed that my variables were all "doubles" when many of the functions in my code produce "single" precision results. So I'm going in this AM to test if changing all the variables above that are doubles to singles will help. (I borrowed this code from someone who shared it with me -- I'm such an idiot when it comes to this stuff.)

Thanks again for your help and please keep following this thread.

MCC

[lookaround] "you cain't fix 'stupid'...
 
I think that my problem is the intemittent "unreadiness" of the printer to accept inputs from the code above. I have decided just to resume where the error happened, like this....

Code:
On error goto ErrorHandler

(some code)

Exit sub
'~~~~~

ErrorHandler:

if intErrorCounter < maxErrorsAllowed then

    Call ErrorLogger(with params)

    Resume

    Err.Clear

end if

End Sub

I tried this and it seems to work perfectly. Thanks for your help and thoughts.

Ortho


[lookaround] "you cain't fix 'stupid'...
 
Regarding your Error Handler, I would strongly suggest checking if it is the error you are expecting before calling Resume, e.g.

Code:
If Err.Number = [Your Error Number] Then
  Resume
Else
  'Whatever you normally do with an "unforeseen" error
End If

What might happen with your current handler is that some totally different error might occur but your procedure keeps on going as if nothing happened. That can lead to bugs that are very hard to figure out.
 
Folks,

I did what JoeAtWork recommended. Found that changing printer orientation was the error that was causing the trouble. So now I just "hammer" (ie, resume) on that command until it works. Usually after 3 times...

Thanks to all,

Ortho

[lookaround] "you cain't fix 'stupid'...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top