By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,101 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,101 IT Pros & Developers. It's quick & easy.

Autoprint screen (no dialog box to select printer)

P: 1
Visual Basic 6.3 - i am auto printing a screen using vb. i dont want the print dialog box to appear. the page should just print to the default printer. alternatively if i could use "sendkeys" to dialog box this would be okay but vb code waits for a reply to the print dialog box.. The line I am getting an error on is: application.activedocument.print - it jumps to the error handler. I have also tried application.activedocument.printform - Note: The application is being made using a process control system which uses VB 6.3 for the GUI, it may be a scaled down version of VB 6.3 - I am not familiar with the printer object, I have tried printer.print but I received an error message saying :variable not defined, I understand what this means, but is the printer object something I can place on the form (like Winsock control) or is it just like a variable that I need to define. I have looked up a few pages on printer object but they havent given me the info i need. Thanks for the help.
Mar 26 '07 #1
Share this Question
Share on Google+
1 Reply

P: 1
Function PrintWrapped(ByVal strInput As String, Optional bPageBreak As Boolean = False, Optional bBreakWords As Boolean = False) As Integer
' strInput - The string to be printed
' bPageBreak - Set this to true for the
' code to check for end of page,
'and wrap to a new page.
' bBreakWords - Set this to True if you

' do not want to break the line
'at a space if possible (i.e. if you don
't care if it
'breaks words into two pieces)
' RETURNS: Number of lines printed. Allo
' ws you to track to end of page
Dim lPointer As Long
Dim lPosition As Long
Dim lPtrWidth As Long
Dim lInputWidth As Long
Dim iLineCount As Integer
Dim strWork As String
lPtrWidth = Printer.ScaleWidth ' Only check this once
lInputWidth = Printer.TextWidth(strInput)

Do While lInputWidth > lPtrWidth
' Estimate breakpoint
lPosition = Int((lPtrWidth / lInputWidth) * Len(strInput))
strWork = Left$(strInput, lPosition)
' Find max that can print on a line
' First, if you have text that is shorte
' r than the max possible, then
' add additional characters to the work
' string

Do While Printer.TextWidth(strWork) < lPtrWidth
If Len(strWork) = Len(strInput) Then Exit Do
strWork = Left$(strInput, Len(strWork) + 2)
' Now trim characters off the work strin
' g until it is shorter than
' the printer width

Do While Printer.TextWidth(strWork) > lPtrWidth
strWork = Left$(strWork, Len(strWork) - 1)
' If you are breaking at spaces, then...

If Not bBreakWords Then

lPointer = Len(strWork)
' ************ VB3 - VB5 Code **********
' *****
' If using VB6, then uncomment the line
' of code
' containing th InStrRev function, and c
' omment
' out this code.
' **************************************
' *****
lPosition = lPointer

' Hunt for a space in the string

Do Until lPosition = 1
If Mid(strWork, lPosition, 1) = " " Then Exit Do
lPosition = lPosition - 1
' ************* VB6 Code **************
' This line replaces the above block for
' *************************************
'lPosition = InStrRev(strWork, " ", lPoi
' nter)
' ************* End Optional Code
' If there is no space before the first
' character, then we will
' be breaking within a word

If lPosition > 1 Then ' Found a space
strWork = Left(strWork, lPosition - 1)

If lPosition < Len(strInput) Then
strInput = Mid(strInput, lPosition + 1)
strInput = ""
End If
Else ' Must break In word
strInput = Mid(strInput, lPointer + 1)
End If

If Len(strInput) > lPointer Then
strInput = Mid(strInput, lPointer + 1)
strInput = ""
End If
End If
Printer.Print strWork
iLineCount = iLineCount + 1
lInputWidth = Printer.TextWidth(strInput)
' Page break if requested

If bPageBreak Then

If Printer.CurrentY > (Printer.ScaleHeight - Printer.TextHeight(strWork)) Then
End If
End If

If Len(strInput) > 0 Then
Printer.Print strInput
iLineCount = iLineCount + 1
End If
PrintWrapped = iLineCount
End Function

Private Sub Command1_Click()
Call PrintWrapped(True, False)
End Sub
Apr 24 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.