Results 1 to 19 of 19

Thread: [RESOLVED] VB Printing

  1. #1

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Resolved [RESOLVED] VB Printing

    Hi,

    I have got some problems when I try to print something out of VB.

    Here is the code i have:

    VB Code:
    1. Private Sub print_Click()
    2.  
    3. Dim a As Object
    4. Set a = Printer
    5.  
    6.  
    7. 'Print
    8. 'Form1
    9. a.Print Std1.Frame3
    10. a.Print Std1.Label3
    11. a.Print Std1.Frame2
    12. a.Print Std1.Label1
    13. a.Print Std1.desc1
    14. Write #1, ""
    15. a.Print Std1.Frame1
    16. a.Print Std1.Label7
    17. a.Print Std1.desc2a
    18. Write #1, ""
    19. 'Form1
    20. 'Form2
    21. a.Print Std2.Frame1
    22. a.Print Std2.Label1
    23. a.Print Std2.desc2b
    24. Write #1, ""
    25. a.Print Std2.Frame2
    26. a.Print Std2.Label4
    27. a.Print Std2.desc3
    28. Write #1, ""
    29. 'Form2
    30. .............
    31. ..............
    32. .........
    33. 'Print
    34.  
    35. testmsg = MsgBox("Successfully Printed..", vbOKOnly, "Printing")
    36.  
    37. End Sub

    This code basically takes the text out of various textboxes and labels and prints them out in order.
    But when I try to print something out, it gives a print error and tries to print everything in one line. How can I word wrap according to paper size?

    Also how can I put formatting things like bold, italics, underline etc etc when printing?

    Please help.

    Cheers!
    Kumar

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: VB Printing

    What is the error you're getting? Why are you writing empty strings to a file with the Write # statement? Is this file opened? Each Print statement should print on a new line unless it is followed by a comma or a semicolon, and you don't have that.

  3. #3

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: VB Printing

    Hi Joacim,

    The empty strings are a mistake, the file #1 is open and for some reason instead of printing empty strings, i decided to write into the file. But that is not the problem. each print statement prints in a new line, no problem with that, But if the contents of a print statement is longer than paper size, it gives a page out of bounds error (I am not sure abt the error, I dont have a printer at home. I can test it 2moro in my lab comp and see what error it is exactly) and tries to print each print statement in one line of the paper. WHat could be the problem?

    Cheers!
    Kumar

    Quote Originally Posted by Joacim Andersson
    What is the error you're getting? Why are you writing empty strings to a file with the Write # statement? Is this file opened? Each Print statement should print on a new line unless it is followed by a comma or a semicolon, and you don't have that.

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: VB Printing

    Add the following Sub to your code. Now instead of calling a.Print Std1.Frame3 and so on simply call WrapLine Std1.Frame3.
    VB Code:
    1. Public Sub WrapLine(ByVal sText As String)
    2.     Dim sWord As String, sChar As String
    3.     Dim i As Long, nCount As Long
    4.     'Replace all CrLf pairs to only Lf
    5.     sText = Replace(sText, vbCrLf, vbLf)
    6.     nCount = Len(sText)
    7.     For i = 1 To nCount
    8.         sChar = Mid$(sText, i, 1)
    9.         Select Case sChar
    10.             Case " ", vbTab, vbLf
    11.                 If Printer.TextWidth(sWord) + Printer.CurrentX > Printer.ScaleWidth Then
    12.                     Printer.Print
    13.                     If sChar = vbLf Then sChar = ""
    14.                     If Printer.CurrentX + Printer.TextHeight(sWord) > Printer.ScaleHeight Then
    15.                         Printer.NewPage
    16.                     End If
    17.                 End If
    18.                 If sChar = vbLf Then sChar = vbCrLf
    19.                 Printer.Print sWord; sChar;
    20.                 sWord = ""
    21.             Case Else
    22.                 sWord = sWord & sChar
    23.         End Select
    24.     Next
    25.     If Len(sWord) Then
    26.         If Printer.CurrentX + Printer.TextWidth(sWord) > Printer.ScaleWidth Then
    27.             Printer.Print
    28.             If Printer.CurrentX + Printer.TextHeight(sWord) > Printer.ScaleHeight Then
    29.                 Printer.NewPage
    30.             End If
    31.         End If
    32.         Printer.Print sWord
    33.     Else
    34.         Printer.Print
    35.     End If
    36. End Sub

  5. #5

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: VB Printing

    That compiled without any errors. Let me try to print 2moro from my lab and see if it works. Meanwhile would you mind explaining what the code does. If it is not a problem to you. Thanks a lot.

    ps: How do i do bold, italics, set font, font size and underline during printing?

    Cheers!
    Kumar

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: VB Printing

    The code simply loops through a sting one character at the time and as long as it doesn't find a whitespace character (space, tab, newline) it will add it to the sWord variable. When a whitespace is encountered it checks if the length of the word + the current printing position is less then the width (or rather scalewidth) of the printer object. If it is less it will print the word on the current line, otherwise it will do a line break and print the word on the next line. If it line breaks it also checks if the word will still fit on the paper, otherwise it will call the NewPage method of the printer object first.

    To change the Font styles you'll need to set Printer.Font.Bold to True or False (or Printer.Font.Italic or set the Printer.Font.Size to a numeric font size). The WrapLine procedure above does nothing of that so you should do that before you send a string to that procedure.

  7. #7

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: VB Printing

    Wonderful. Thanks a bunch. I still have not checked with a printer. I will do so 2moro. If it works, which i am sure it will, I owe u one

    THanks a lot

    Cheers!
    Kumar

  8. #8

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: VB Printing

    Meanwhile, I have another problem. I know this will sound silly, but when i close the x button of my form why doesnt it end the program? It keeps running in the background. Why is that? How can I end the program by pressing the x button?

    please help

    THanks!

  9. #9
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: VB Printing

    in your form's unload event write End statement...if you do not want the user to close the app using x button then write
    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)
    2.     Cancel = -1
    3.     'if you want to close the app, then comment the above statement and write End here
    4. End Sub
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  10. #10

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: VB Printing

    Thanks mate, That wordwrap worked. Super thanks.

    Cheers!


    Quote Originally Posted by Joacim Andersson
    The code simply loops through a sting one character at the time and as long as it doesn't find a whitespace character (space, tab, newline) it will add it to the sWord variable. When a whitespace is encountered it checks if the length of the word + the current printing position is less then the width (or rather scalewidth) of the printer object. If it is less it will print the word on the current line, otherwise it will do a line break and print the word on the next line. If it line breaks it also checks if the word will still fit on the paper, otherwise it will call the NewPage method of the printer object first.

    To change the Font styles you'll need to set Printer.Font.Bold to True or False (or Printer.Font.Italic or set the Printer.Font.Size to a numeric font size). The WrapLine procedure above does nothing of that so you should do that before you send a string to that procedure.

  11. #11

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: VB Printing

    That worked. Thanks a lot GaneshMoorty. Are you from India? Me too.

    CHeers!
    Kumar

    Quote Originally Posted by ganeshmoorthy
    in your form's unload event write End statement...if you do not want the user to close the app using x button then write
    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)
    2.     Cancel = -1
    3.     'if you want to close the app, then comment the above statement and write End here
    4. End Sub

  12. #12
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: VB Printing

    if you got answer to your post please mark this as resolved and rate it..
    BTW: which part of india you belong to...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: VB Printing

    I would not recommend you to use the End keyword. The reason for that is that End will shut down your application abnormally, you should instead investigate the reason that your application doesn't shut itself down when you close the Form. The most common reason is that you have more than one Form in your application and that one of these are still loaded into memory. Another reason might be that you're accessing the Form after it supposed to be closed. You might have a loop somewhere that are still running.

  14. #14
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: VB Printing

    You may still have references (forms or other objects) which have not been set to nothing.

    Now that you've experienced what it does... I think it would be best you start keeping track of your references and releasing them when your done using them.

  15. #15

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: VB Printing

    leinad31 and Joacim, My program does have couple of other forms that keeps running in the background when some particular form is shown. So how can i release the memory and close all forms before I shutdown? I am pretty new to VB and I have no clue. Thnks for bringing that up.

    Cheers!
    Kumar

    Quote Originally Posted by Joacim Andersson
    I would not recommend you to use the End keyword. The reason for that is that End will shut down your application abnormally, you should instead investigate the reason that your application doesn't shut itself down when you close the Form. The most common reason is that you have more than one Form in your application and that one of these are still loaded into memory. Another reason might be that you're accessing the Form after it supposed to be closed. You might have a loop somewhere that are still running.

  16. #16

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: VB Printing

    I am from South India (Trichy) but now I live in MI. Wat abt u?

    CHeers!
    Kumar

    Quote Originally Posted by ganeshmoorthy
    if you got answer to your post please mark this as resolved and rate it..
    BTW: which part of india you belong to...

  17. #17
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: VB Printing

    I'm from Chennai, working for an MNC here as Smart Card Engineer...what abt u....u can pm me...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  18. #18
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: VB Printing

    Quote Originally Posted by midway
    My program does have couple of other forms that keeps running in the background when some particular form is shown. So how can i release the memory and close all forms before I shutdown? I am pretty new to VB and I have no clue. Thnks for bringing that up.
    In that case you've probably haven't unloaded those Forms when you close your app, and that is what keeps it running. What you can do is to simply loop through the Forms collection and unload each of the Forms when the main Form is closed. You can do this in the Form_Unload event of your main Form.
    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)
    2.     Dim frm As Form
    3.  
    4.     For Each frm In Forms
    5.         Unload frm
    6.     Next
    7.     Set frm = Nothing
    8. End Sub

  19. #19

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: VB Printing

    Super, that works great. Thanks a lot.

    Quote Originally Posted by Joacim Andersson
    In that case you've probably haven't unloaded those Forms when you close your app, and that is what keeps it running. What you can do is to simply loop through the Forms collection and unload each of the Forms when the main Form is closed. You can do this in the Form_Unload event of your main Form.
    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)
    2.     Dim frm As Form
    3.  
    4.     For Each frm In Forms
    5.         Unload frm
    6.     Next
    7.     Set frm = Nothing
    8. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width