Results 1 to 16 of 16

Thread: Printing

  1. #1

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Printing

    Hey All,

    I Have It To Print What is In My RichTextbox, but it goes straight to the default printer, so i want it so people can choose the printer,

    how would i do this

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing

    Put a combobox on a form, and then use this. The Printer Dialog doesn't work very well.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Combo1_Click()
    4.   Dim p As String
    5.   Dim prt As Printer
    6.   p = Combo1.List(Combo1.ListIndex)
    7.   For Each prt In Printers
    8.     If prt.DeviceName = p Then
    9.     Set Printer = prt
    10.     End If
    11.   Next prt
    12. End Sub
    13.  
    14. Private Sub Form_Load()
    15.   Dim x%
    16.   Dim prt As Printer
    17.   Combo1.Clear
    18.   For Each prt In Printers
    19.     Combo1.AddItem prt.DeviceName
    20.   Next prt
    21.   For x = 0 To Combo1.ListCount
    22.       If Combo1.List(x) = Printer.DeviceName Then
    23.       Combo1.TopIndex = x
    24.       Combo1.ListIndex = x
    25.       Exit Sub
    26.     End If
    27.   Next x
    28. End Sub

  3. #3

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing

    how do i put a command button in there so that it prints when they press it

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing

    You might want to use this. It will call up the print dialog.

    I thought that you were printing already? The combo should be in a setup routine.

    http://support.microsoft.com/kb/322710/EN-US/#7

  5. #5

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing

    thanx for directing me to this but...

    this is the code for my form with the button in to bring up the printer dialogue box

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim printDlg As Printer
    3. Set printDlg = New PrinterDlg
    4. ' Set the starting information for the dialog box based on the current
    5. ' printer settings.
    6. printDlg[COLOR=Yellow].PrinterName[/COLOR] = Printer.DeviceName
    7. printDlg.DriverName = Printer.DriverName
    8. printDlg.Port = Printer.Port
    9.  
    10. ' Set the default PaperBin so that a valid value is returned even
    11. ' in the Cancel case.
    12. printDlg.PaperBin = Printer.PaperBin
    13.  
    14. ' Set the flags for the PrinterDlg object using the same flags as in the
    15. ' common dialog control. The structure starts with VBPrinterConstants.
    16. printDlg.Flags = VBPrinterConstants.cdlPDNoSelection _
    17.                  Or VBPrinterConstants.cdlPDNoPageNums _
    18.                  Or VBPrinterConstants.cdlPDReturnDC
    19. Printer.TrackDefault = False
    20.  
    21. ' When CancelError is set to True the ShowPrinterDlg will return error
    22. ' 32755. You can handle the error to know when the Cancel button was
    23. ' clicked. Enable this by uncommenting the lines prefixed with "'**".
    24. '**printDlg.CancelError = True
    25.  
    26. ' Add error handling for Cancel.
    27. '**On Error GoTo Cancel
    28. If Not printDlg.ShowPrinter(Me.hWnd) Then
    29.     Debug.Print "Cancel Selected"
    30.     Exit Sub
    31. End If
    32.  
    33. 'Turn off Error Handling for Cancel.
    34. '**On Error GoTo 0
    35. Dim NewPrinterName As String
    36. Dim objPrinter As Printer
    37. Dim strsetting As String
    38.  
    39. ' Locate the printer that the user selected in the Printers collection.
    40. NewPrinterName = UCase$(printDlg.PrinterName)
    41. If Printer.DeviceName <> NewPrinterName Then
    42.     For Each objPrinter In Printers
    43.        If UCase$(objPrinter.DeviceName) = NewPrinterName Then
    44.             Set Printer = objPrinter
    45.        End If
    46.     Next
    47. End If
    48.  
    49. ' Copy user input from the dialog box to the properties of the selected printer.
    50. Printer.Copies = printDlg.Copies
    51. Printer.Orientation = printDlg.Orientation
    52. Printer.ColorMode = printDlg.ColorMode
    53. Printer.Duplex = printDlg.Duplex
    54. Printer.PaperBin = printDlg.PaperBin
    55. Printer.PaperSize = printDlg.PaperSize
    56. Printer.PrintQuality = printDlg.PrintQuality
    57.  
    58. ' Display the results in the immediate (Debug) window.
    59. ' NOTE: Supported values for PaperBin and Size are printer specific. Some
    60. ' common defaults are defined in the Win32 SDK in MSDN and in Visual Basic.
    61. ' Print quality is the number of dots per inch.
    62. With Printer
    63.     Debug.Print .DeviceName
    64.     If .Orientation = 1 Then
    65.         strsetting = "Portrait. "
    66.     Else
    67.         strsetting = "Landscape. "
    68.     End If
    69.     Debug.Print "Copies = " & .Copies, "Orientation = " & _
    70.        strsetting
    71.     If .ColorMode = 1 Then
    72.         strsetting = "Black and White. "
    73.     Else
    74.         strsetting = "Color. "
    75.     End If
    76.     Debug.Print "ColorMode = " & strsetting
    77.     If .Duplex = 1 Then
    78.         strsetting = "None. "
    79.     ElseIf .Duplex = 2 Then
    80.         strsetting = "Horizontal/Long Edge. "
    81.     ElseIf .Duplex = 3 Then
    82.         strsetting = "Vertical/Short Edge. "
    83.     Else
    84.         strsetting = "Unknown. "
    85.     End If
    86.     Debug.Print "Duplex = " & strsetting
    87.     Debug.Print "PaperBin = " & .PaperBin
    88.     Debug.Print "PaperSize = " & .PaperSize
    89.     Debug.Print "PrintQuality = " & .PrintQuality
    90.     If (printDlg.Flags And VBPrinterConstants.cdlPDPrintToFile) = _
    91.        VBPrinterConstants.cdlPDPrintToFile Then
    92.          Debug.Print "Print to File Selected"
    93.     Else
    94.          Debug.Print "Print to File Not Selected"
    95.     End If
    96.     Debug.Print "hDC = " & printDlg.hdc
    97. End With
    98. Exit Sub
    99. '**Cancel:
    100. '**If Err.Number = 32755 Then
    101. '**    Debug.Print "Cancel Selected"
    102. '**Else
    103. '**    Debug.Print "A nonCancel Error Occured - "; Err.Number
    104. '**End If
    105. End Sub

    but when i try to run it, it highlights the 5th line (in color yellow) and says

    Compile Error: Method Or Data Member Not Found

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing

    Did you download and install the control? Unzip the files, and copy them to the \windows\system32 folder. Then from a command prompt, type

    VB Code:
    1. regsvr32 VBprndlg.dll

    You should get a message that it succeded. Then you have to restart VB and press Project - References to add the control to the IDE. Then, double-click it to add it to your form.

    Let us know if you have any other problems.

    Better yet, I've made it into a project. Once everthing is registered, just run this program to see everthing in operation. This worked fine for me.
    Attached Files Attached Files
    Last edited by dglienna; Jun 19th, 2005 at 02:22 PM.

  7. #7

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing

    1. What Is The IDE

    2. Yes I Have Run The Dll File

    3. What Is The Name Of The Thing I Am Looking For When I Press Ctrl T

    Thanx

    EDIT: Seems To Be Working Now, I Will Post If I Get Any Problems

    Thanx Again

    Lavarock09

  8. #8
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Printing

    Quote Originally Posted by lavarock09
    1. What Is The IDE

    2. Yes I Have Run The Dll File

    3. What Is The Name Of The Thing I Am Looking For When I Press Ctrl T

    Thanx

    EDIT: Seems To Be Working Now, I Will Post If I Get Any Problems

    Thanx Again

    Lavarock09

    Just to expand on what Dave posted the following code displays the Printer Dialog and the Page Dialog:

    VB Code:
    1. 'This project needs 2 command buttons
    2. Option Explicit
    3. Const FW_NORMAL = 400
    4. Const DEFAULT_CHARSET = 1
    5. Const OUT_DEFAULT_PRECIS = 0
    6. Const CLIP_DEFAULT_PRECIS = 0
    7. Const DEFAULT_QUALITY = 0
    8. Const DEFAULT_PITCH = 0
    9. Const FF_ROMAN = 16
    10. Const CF_PRINTERFONTS = &H2
    11. Const CF_SCREENFONTS = &H1
    12. Const CF_BOTH = (CF_SCREENFONTS Or CF_PRINTERFONTS)
    13. Const CF_EFFECTS = &H100&
    14. Const CF_FORCEFONTEXIST = &H10000
    15. Const CF_INITTOLOGFONTSTRUCT = &H40&
    16. Const CF_LIMITSIZE = &H2000&
    17. Const REGULAR_FONTTYPE = &H400
    18. Const LF_FACESIZE = 32
    19. Const CCHDEVICENAME = 32
    20. Const CCHFORMNAME = 32
    21. Const GMEM_MOVEABLE = &H2
    22. Const GMEM_ZEROINIT = &H40
    23. Const DM_DUPLEX = &H1000&
    24. Const DM_ORIENTATION = &H1&
    25. Const PD_PRINTSETUP = &H40
    26. Const PD_DISABLEPRINTTOFILE = &H80000
    27. Private Type POINTAPI
    28.     x As Long
    29.     y As Long
    30. End Type
    31. Private Type RECT
    32.     Left As Long
    33.     Top As Long
    34.     Right As Long
    35.     Bottom As Long
    36. End Type
    37. Private Type OPENFILENAME
    38.     lStructSize As Long
    39.     hwndOwner As Long
    40.     hInstance As Long
    41.     lpstrFilter As String
    42.     lpstrCustomFilter As String
    43.     nMaxCustFilter As Long
    44.     nFilterIndex As Long
    45.     lpstrFile As String
    46.     nMaxFile As Long
    47.     lpstrFileTitle As String
    48.     nMaxFileTitle As Long
    49.     lpstrInitialDir As String
    50.     lpstrTitle As String
    51.     flags As Long
    52.     nFileOffset As Integer
    53.     nFileExtension As Integer
    54.     lpstrDefExt As String
    55.     lCustData As Long
    56.     lpfnHook As Long
    57.     lpTemplateName As String
    58. End Type
    59. Private Type PAGESETUPDLG
    60.     lStructSize As Long
    61.     hwndOwner As Long
    62.     hDevMode As Long
    63.     hDevNames As Long
    64.     flags As Long
    65.     ptPaperSize As POINTAPI
    66.     rtMinMargin As RECT
    67.     rtMargin As RECT
    68.     hInstance As Long
    69.     lCustData As Long
    70.     lpfnPageSetupHook As Long
    71.     lpfnPagePaintHook As Long
    72.     lpPageSetupTemplateName As String
    73.     hPageSetupTemplate As Long
    74. End Type
    75. Private Type PRINTDLG_TYPE
    76.     lStructSize As Long
    77.     hwndOwner As Long
    78.     hDevMode As Long
    79.     hDevNames As Long
    80.     hDC As Long
    81.     flags As Long
    82.     nFromPage As Integer
    83.     nToPage As Integer
    84.     nMinPage As Integer
    85.     nMaxPage As Integer
    86.     nCopies As Integer
    87.     hInstance As Long
    88.     lCustData As Long
    89.     lpfnPrintHook As Long
    90.     lpfnSetupHook As Long
    91.     lpPrintTemplateName As String
    92.     lpSetupTemplateName As String
    93.     hPrintTemplate As Long
    94.     hSetupTemplate As Long
    95. End Type
    96. Private Type DEVNAMES_TYPE
    97.     wDriverOffset As Integer
    98.     wDeviceOffset As Integer
    99.     wOutputOffset As Integer
    100.     wDefault As Integer
    101.     extra As String * 100
    102. End Type
    103. Private Type DEVMODE_TYPE
    104.     dmDeviceName As String * CCHDEVICENAME
    105.     dmSpecVersion As Integer
    106.     dmDriverVersion As Integer
    107.     dmSize As Integer
    108.     dmDriverExtra As Integer
    109.     dmFields As Long
    110.     dmOrientation As Integer
    111.     dmPaperSize As Integer
    112.     dmPaperLength As Integer
    113.     dmPaperWidth As Integer
    114.     dmScale As Integer
    115.     dmCopies As Integer
    116.     dmDefaultSource As Integer
    117.     dmPrintQuality As Integer
    118.     dmColor As Integer
    119.     dmDuplex As Integer
    120.     dmYResolution As Integer
    121.     dmTTOption As Integer
    122.     dmCollate As Integer
    123.     dmFormName As String * CCHFORMNAME
    124.     dmUnusedPadding As Integer
    125.     dmBitsPerPel As Integer
    126.     dmPelsWidth As Long
    127.     dmPelsHeight As Long
    128.     dmDisplayFlags As Long
    129.     dmDisplayFrequency As Long
    130. End Type
    131. Private Declare Function PrintDialog Lib "comdlg32.dll" Alias "PrintDlgA" (pPrintdlg As PRINTDLG_TYPE) As Long
    132. Private Declare Function PAGESETUPDLG Lib "comdlg32.dll" Alias "PageSetupDlgA" (pPagesetupdlg As PAGESETUPDLG) As Long
    133. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    134. Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    135. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    136. Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    137. Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
    138.  
    139. Private Sub Command1_Click()
    140.     ShowPrinter Me
    141. End Sub
    142.  
    143. Private Sub Command2_Click()
    144.     ShowPageSetupDlg
    145. End Sub
    146.  
    147. Private Sub Form_Load()
    148.     'KPD-Team 1998
    149.     'URL: [url]http://www.allapi.net/[/url]
    150.     'E-Mail: [email][email protected][/email]
    151.     'Redim the variables to store the cutstom colors
    152.    
    153.     'Set the captions
    154.     Command1.Caption = "ShowPrinter"
    155.     Command2.Caption = "ShowPageSetupDlg"
    156. End Sub
    157.  
    158. Private Function ShowPageSetupDlg() As Long
    159.     Dim m_PSD As PAGESETUPDLG
    160.     'Set the structure size
    161.     m_PSD.lStructSize = Len(m_PSD)
    162.     'Set the owner window
    163.     m_PSD.hwndOwner = Me.hWnd
    164.     'Set the application instance
    165.     m_PSD.hInstance = App.hInstance
    166.     'no extra flags
    167.     m_PSD.flags = 0
    168.  
    169.     'Show the pagesetup dialog
    170.     If PAGESETUPDLG(m_PSD) Then
    171.         ShowPageSetupDlg = 0
    172.     Else
    173.         ShowPageSetupDlg = -1
    174.     End If
    175. End Function
    176.  
    177. Public Sub ShowPrinter(frmOwner As Form, Optional PrintFlags As Long)
    178.     '-> Code by Donald Grover
    179.     Dim PrintDlg As PRINTDLG_TYPE
    180.     Dim DevMode As DEVMODE_TYPE
    181.     Dim DevName As DEVNAMES_TYPE
    182.  
    183.     Dim lpDevMode As Long, lpDevName As Long
    184.     Dim bReturn As Integer
    185.     Dim objPrinter As Printer, NewPrinterName As String
    186.  
    187.     ' Use PrintDialog to get the handle to a memory
    188.     ' block with a DevMode and DevName structures
    189.  
    190.     PrintDlg.lStructSize = Len(PrintDlg)
    191.     PrintDlg.hwndOwner = frmOwner.hWnd
    192.  
    193.     PrintDlg.flags = PrintFlags
    194.     On Error Resume Next
    195.     'Set the current orientation and duplex setting
    196.     DevMode.dmDeviceName = Printer.DeviceName
    197.     DevMode.dmSize = Len(DevMode)
    198.     DevMode.dmFields = DM_ORIENTATION Or DM_DUPLEX
    199.     DevMode.dmPaperWidth = Printer.Width
    200.     DevMode.dmOrientation = Printer.Orientation
    201.     DevMode.dmPaperSize = Printer.PaperSize
    202.     DevMode.dmDuplex = Printer.Duplex
    203.     On Error GoTo 0
    204.  
    205.     'Allocate memory for the initialization hDevMode structure
    206.     'and copy the settings gathered above into this memory
    207.     PrintDlg.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevMode))
    208.     lpDevMode = GlobalLock(PrintDlg.hDevMode)
    209.     If lpDevMode > 0 Then
    210.         CopyMemory ByVal lpDevMode, DevMode, Len(DevMode)
    211.         bReturn = GlobalUnlock(PrintDlg.hDevMode)
    212.     End If
    213.  
    214.     'Set the current driver, device, and port name strings
    215.     With DevName
    216.         .wDriverOffset = 8
    217.         .wDeviceOffset = .wDriverOffset + 1 + Len(Printer.DriverName)
    218.         .wOutputOffset = .wDeviceOffset + 1 + Len(Printer.Port)
    219.         .wDefault = 0
    220.     End With
    221.  
    222.     With Printer
    223.         DevName.extra = .DriverName & Chr(0) & .DeviceName & Chr(0) & .Port & Chr(0)
    224.     End With
    225.  
    226.     'Allocate memory for the initial hDevName structure
    227.     'and copy the settings gathered above into this memory
    228.     PrintDlg.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevName))
    229.     lpDevName = GlobalLock(PrintDlg.hDevNames)
    230.     If lpDevName > 0 Then
    231.         CopyMemory ByVal lpDevName, DevName, Len(DevName)
    232.         bReturn = GlobalUnlock(lpDevName)
    233.     End If
    234.  
    235.     'Call the print dialog up and let the user make changes
    236.     If PrintDialog(PrintDlg) <> 0 Then
    237.  
    238.         'First get the DevName structure.
    239.         lpDevName = GlobalLock(PrintDlg.hDevNames)
    240.         CopyMemory DevName, ByVal lpDevName, 45
    241.         bReturn = GlobalUnlock(lpDevName)
    242.         GlobalFree PrintDlg.hDevNames
    243.  
    244.         'Next get the DevMode structure and set the printer
    245.         'properties appropriately
    246.         lpDevMode = GlobalLock(PrintDlg.hDevMode)
    247.         CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
    248.         bReturn = GlobalUnlock(PrintDlg.hDevMode)
    249.         GlobalFree PrintDlg.hDevMode
    250.         NewPrinterName = UCase$(Left(DevMode.dmDeviceName, InStr(DevMode.dmDeviceName, Chr$(0)) - 1))
    251.         If Printer.DeviceName <> NewPrinterName Then
    252.             For Each objPrinter In Printers
    253.                 If UCase$(objPrinter.DeviceName) = NewPrinterName Then
    254.                     Set Printer = objPrinter
    255.                     'set printer toolbar name at this point
    256.                 End If
    257.             Next
    258.         End If
    259.  
    260.         On Error Resume Next
    261.         'Set printer object properties according to selections made
    262.         'by user
    263.         Printer.Copies = DevMode.dmCopies
    264.         Printer.Duplex = DevMode.dmDuplex
    265.         Printer.Orientation = DevMode.dmOrientation
    266.         Printer.PaperSize = DevMode.dmPaperSize
    267.         Printer.PrintQuality = DevMode.dmPrintQuality
    268.         Printer.ColorMode = DevMode.dmColor
    269.         Printer.PaperBin = DevMode.dmDefaultSource
    270.         On Error GoTo 0
    271.     End If
    272. End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing

    M$ finally wrapped that up into a control. It comes in handy!

  10. #10

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing

    thank you but...(theres always a but isn't there)

    how do i make it so it prints what is in my richtextbox?

  11. #11
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Printing

    Quote Originally Posted by dglienna
    M$ finally wrapped that up into a control. It comes in handy!
    Dave,
    I have d/l the new Dialog control, so do I need to still use all of that mess that I posted? Also How do I print to a specific area in a RTB?
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  12. #12
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Printing

    Quote Originally Posted by lavarock09
    thank you but...(theres always a but isn't there)

    how do i make it so it prints what is in my richtextbox?
    Lavarock,
    I am attempting to write my own report writer using a RTB and I have found the following code to print the contains of a RTB:

    VB Code:
    1. Option Explicit
    2. Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    3. Private Const EM_GETLINECOUNT = &HBA
    4. Private Const WM_PASTE = &H302
    5.  
    6. Public intPageNumber As Integer
    7.  
    8. Public Const WM_USER As Long = &H400
    9. Public Const EM_FORMATRANGE As Long = WM_USER + 57
    10. Public Const EM_SETTARGETDEVICE As Long = WM_USER + 72
    11. Public Const PHYSICALOFFSETX As Long = 112
    12. Public Const PHYSICALOFFSETY As Long = 113
    13.  
    14. Public Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
    15. Public Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As Long, ByVal lpInitData As Long) As Long
    16.  
    17. Public Type Rect
    18.   Left As Long
    19.   Top As Long
    20.   Right As Long
    21.   Bottom As Long
    22. End Type
    23.  
    24. Public Type CharRange
    25.   cpMin As Long ' First character of range (0 for start of doc)
    26.   cpMax As Long ' Last character of range (-1 for end of doc)
    27. End Type
    28.  
    29. Private Type FormatRange
    30.   hdc As Long ' Actual DC to draw on
    31.   hdcTarget As Long ' Target DC for determining text formatting
    32.   rc As Rect ' Region of the DC to draw to (in twips)
    33.   rcPage As Rect ' Region of the entire DC (page size) (in twips)
    34.   chrg As CharRange ' Range of text to draw (see above declaration)
    35. End Type
    36.  
    37. Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long, _
    38.                         TopMarginHeight, RightMarginWidth, BottomMarginHeight, _
    39.                         Optional blnPrintPageNum As Boolean = False)
    40.  
    41. Dim LeftOffset As Long
    42. Dim TopOffset As Long
    43. Dim LeftMargin As Long
    44. Dim TopMargin As Long
    45. Dim RightMargin As Long
    46. Dim BottomMargin As Long
    47. Dim fr As FormatRange
    48. Dim rcDrawTo As Rect
    49. Dim rcPage As Rect
    50. Dim TextLength As Long
    51. Dim NextCharPosition As Long
    52. Dim r As Long
    53. Dim strPageText As String
    54.  
    55. ' Start a print job to get a valid Printer.hDC
    56. Printer.Print Space(1)
    57. 'strOrientation
    58. Printer.ScaleMode = vbTwips
    59.  
    60. ' Get the offsett to the printable area on the page in twips
    61. LeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, PHYSICALOFFSETX), vbPixels, vbTwips)
    62. TopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hdc, PHYSICALOFFSETY), vbPixels, vbTwips)
    63.  
    64. ' Calculate the Left, Top, Right, and Bottom margins
    65. LeftMargin = LeftMarginWidth - LeftOffset
    66. TopMargin = TopMarginHeight - TopOffset
    67. RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset
    68. BottomMargin = (Printer.Height - BottomMarginHeight) - TopOffset
    69.  
    70. ' Set printable area rect
    71. rcPage.Left = 0
    72. rcPage.Top = 0
    73. rcPage.Right = Printer.ScaleWidth
    74. rcPage.Bottom = Printer.ScaleHeight
    75.  
    76. ' Set rect in which to print (relative to printable area)
    77. rcDrawTo.Left = LeftMargin
    78. rcDrawTo.Top = TopMargin
    79. rcDrawTo.Right = RightMargin
    80. rcDrawTo.Bottom = BottomMargin
    81.  
    82. ' Set up the print instructions
    83. fr.hdc = Printer.hdc ' Use the same DC for measuring and rendering
    84. fr.hdcTarget = Printer.hdc ' Point at printer hDC
    85. fr.rc = rcDrawTo ' Indicate the area on page to draw to
    86. fr.rcPage = rcPage ' Indicate entire size of page
    87. fr.chrg.cpMin = 0 ' Indicate start of text through
    88. fr.chrg.cpMax = -1 ' end of the text
    89.  
    90. ' Get length of text in RTF
    91. TextLength = Len(RTF.Text)
    92.  
    93. ' Loop printing each page until done
    94. Do
    95. ' Print the page by sending EM_FORMATRANGE message
    96. NextCharPosition = SendMessage(RTF.hWnd, EM_FORMATRANGE, True, fr)
    97. If NextCharPosition >= TextLength Then Exit Do 'If done then exit
    98. fr.chrg.cpMin = NextCharPosition ' Starting position for next page
    99.  
    100. 'Printer.CurrentX = Printer.ScaleWidth - Len("Page " & intPageNumber)
    101.  
    102. If blnPrintPageNum Then 'This printer the page number in the lower righthand column of every page
    103.     strPageText = "Page " & intPageNumber
    104.     Printer.CurrentX = RightMargin - Len(strPageText)
    105.     Printer.CurrentY = BottomMargin
    106.     Printer.Print strPageText
    107.     intPageNumber = intPageNumber + 1
    108. End If
    109.  
    110. Printer.NewPage ' Move on to next page
    111.  
    112. Printer.Print Space(1) ' Re-initialize hDC
    113.  
    114.  
    115. fr.hdc = Printer.hdc
    116. fr.hdcTarget = Printer.hdc
    117. Loop
    118.  
    119. ' Commit the print job
    120. 'Printer.CurrentX = Printer.ScaleWidth - Len("Page " & intPageNumber)
    121.  
    122. If blnPrintPageNum Then 'This printer the page number in the lower righthand column of every page
    123.     strPageText = "Page " & intPageNumber
    124.     Printer.CurrentX = RightMargin - Len(strPageText)
    125.     Printer.CurrentY = BottomMargin
    126.     Printer.Print strPageText
    127. End If
    128.  
    129. Printer.EndDoc
    130.  
    131. ' Allow the RTF to free up memory
    132. r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal CLng(0))
    133. End Sub

    ... and you call it like this:

    VB Code:
    1. PrintRTF RichTextBox1, 1440, 1440, 1440, 1440, True  ' 1440 Twips = 1 Inch

    The 1440 are the number of twips for each of the margins (1440 twips equals one inch).

    Hope this helps!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  13. #13

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing

    sorry, i am confused, A Lot!

    What Do I Do?

  14. #14

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing

    ok, now i have it done, i cant choose which printer

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing

    I've just added a richtextbox to my example, called rtb.
    Add this line to the Print button

    VB Code:
    1. Debug.Print "hDC = " & printDlg.hDC
    2. End With
    3. [B]rtb.SelPrint printDlg.hDC
    4. [/B]Exit Sub

  16. #16

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing

    my print button code is now

    VB Code:
    1. Private Sub Command1_Click()
    2. PrintRTF Form1.RichTextBox1, 1440, 1440, 1440, 1440, True  ' 1440 Twips = 1 Inch
    3. Debug.Print "hDC = " & PrintDlg.hdc
    4. Form1.RichTextBox1.SelPrint PrintDlg.hdc
    5. End Exit
    6. End Sub

    and that is just wrong

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