Solved

Printing RTF with Table

Posted on 2008-10-17
3
958 Views
Last Modified: 2013-12-15
I am trying to print an RTF, i have an RTF TextBox on my VB6 form. with a command button code attached.

my RTF file has a table i can see it on screen formatted properly but when i print i get laods of code priting rather than my form, as if i had opened the RTF in notepad.

Any ideas how i can fix this, Kindest Regards


Public Sub Command1_Click()

   ' Print the contents of the RichTextBox with a one inch margin

 '  PrintRTF Doc, AnInch, AnInch, AnInch, AnInch

   

Combo1.Text = "HP LaserJet 1018"

Dim Y As Printer

For Each Y In Printers

  If Y.DeviceName = Combo1 Then

     ' Set printer as system default.

     Set Printer = Y

     ' Stop looking for a printer.

     Exit For

  End If

Next
 

'Printer.ForeColor = &H80000008 'sets the text colour

'Printer.Orientation = vbproplandscape

Printer.Print Doc 'prints whats in text1

Printer.EndDoc

End Sub

Open in new window

0
Comment
Question by:Benji_
  • 2
3 Comments
 

Accepted Solution

by:
twiglatte earned 250 total points
ID: 22746900
Add the attached code to a module, then call the sub like this:

PrintRTF RichTextBox1, 1440, 1440, 1440, 1440 ' 1440 Twips = 1 Inch

where RichTextBox1 is your RTF control
'Put the following in a module:
 

Private Declare Function GetDeviceCaps Lib "gdi32" ( _

      ByVal hdc As Long, ByVal nIndex As Long) As Long
 

Private Declare Function SendMessage Lib "USER32" _

      Alias "SendMessageA" _

      (ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, _

      lp As Any) As Long

      

Private Const WM_USER As Long = &H400

Private Const EM_FORMATRANGE As Long = WM_USER + 57

Private Const EM_SETTARGETDEVICE As Long = WM_USER + 72

Private Const PHYSICALOFFSETX As Long = 112

Private Const PHYSICALOFFSETY As Long = 113
 

Private Type Rect

   Left As Long

   Top As Long

   Right As Long

   Bottom As Long

End Type

   

Private Type CharRange

  cpMin As Long     ' First character of range (0 for start of doc)

  cpMax As Long     ' Last character of range (-1 for end of doc)

End Type

   

Private Type FormatRange

  hdc As Long       ' Actual DC to draw on

  hdcTarget As Long ' Target DC for determining text formatting

  rc As Rect        ' Region of the DC to draw to (in twips)

  rcPage As Rect    ' Region of the entire DC (page size) (in twips)

  chrg As CharRange ' Range of text to draw (see above declaration)

End Type

   
 
 
 

Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long, TopMarginHeight As Long, RightMarginWidth As Long, BottomMarginHeight As Long)

      Dim LeftOffset As Long, TopOffset As Long

      Dim LeftMargin As Long, TopMargin As Long

      Dim RightMargin As Long, BottomMargin As Long

      Dim fr As FormatRange

      Dim rcDrawTo As Rect

      Dim rcPage As Rect

      Dim TextLength As Long

      Dim NextCharPosition As Long

      Dim r As Long
 

      ' Start a print job to get a valid Printer.hDC

      Printer.Print Space(1)

      Printer.ScaleMode = vbTwips
 

      ' Get the offsett to the printable area on the page in twips

      LeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, _

         PHYSICALOFFSETX), vbPixels, vbTwips)

      TopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hdc, _

         PHYSICALOFFSETY), vbPixels, vbTwips)
 

      ' Calculate the Left, Top, Right, and Bottom margins

      LeftMargin = LeftMarginWidth - LeftOffset

      TopMargin = TopMarginHeight - TopOffset

      RightMargin = (Printer.Width - RightMarginWidth) - LeftOffset

      BottomMargin = (Printer.Height - BottomMarginHeight) - TopOffset
 

      ' Set printable area rect

      rcPage.Left = 0

      rcPage.Top = 0

      rcPage.Right = Printer.ScaleWidth

      rcPage.Bottom = Printer.ScaleHeight
 

      ' Set rect in which to print (relative to printable area)

      rcDrawTo.Left = LeftMargin

      rcDrawTo.Top = TopMargin

      rcDrawTo.Right = RightMargin

      rcDrawTo.Bottom = BottomMargin
 

      ' Set up the print instructions

      fr.hdc = Printer.hdc   ' Use the same DC for measuring and rendering

      fr.hdcTarget = Printer.hdc  ' Point at printer hDC

      fr.rc = rcDrawTo            ' Indicate the area on page to draw to

      fr.rcPage = rcPage          ' Indicate entire size of page

      fr.chrg.cpMin = 0           ' Indicate start of text through

      fr.chrg.cpMax = -1          ' end of the text
 

      ' Get length of text in RTF

      TextLength = Len(RTF.Text)
 

      ' Loop printing each page until done

      Do

         ' Print the page by sending EM_FORMATRANGE message

         NextCharPosition = SendMessage(RTF.hWnd, _

             EM_FORMATRANGE, True, fr)

         If NextCharPosition >= TextLength Then Exit Do  'If done then exit

         fr.chrg.cpMin = NextCharPosition ' Starting position for next page

         Printer.NewPage                  ' Move on to next page

         Printer.Print Space(1) ' Re-initialize hDC

         fr.hdc = Printer.hdc

         fr.hdcTarget = Printer.hdc

      Loop
 

      ' Commit the print job

      Printer.EndDoc
 

      ' Allow the RTF to free up memory

      r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal _

    CLng(0))

End Sub

Open in new window

0
 
LVL 2

Author Comment

by:Benji_
ID: 22747533
Thanks for that, could ya help me to tell me how i can modifty the code so that i can say which printer,

Because differnt parts of my application will need differnt printers. but i want it type the name myself  into the code.


Regards
0
 
LVL 2

Author Closing Comment

by:Benji_
ID: 31507111
Thanks for the help, adding that and implementing i then used set app default printer frm my code to use the printer i wished to use.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now