473,626 Members | 3,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with code, please

I found some VB code to printout a richtext field and I'm trying to
adapt it to MS Access 2000. I think there might be some small changes
to make the adaption between VB versions. Also my OS is Windows XP.
The website I found the code is:
http://www.developerfusion.com/show/244/ See the code below. There is
also some code found at http://www.freevbcode.com/ShowCode.asp?ID=1028
I would appreciate someone's help in getting this to work.

Instructions and Code:
=============== =============
1. Start a new project in the Visual Basic 32-bit edition. Form1 is
created by default.

2. Put a CommandButton and a RichTextBox control on Form1.

3. Add the following code to Form1:
=============== =============== =============== =============== =====
Option Explicit

Private Sub Form_Load()
Dim LineWidth As Long

' Initialize Form and Command button
Me.Caption = "Rich Text Box WYSIWYG Printing Example"
Command1.Move 10, 10, 600, 380
Command1.Captio n = "&Print"

' Set the font of the RTF to a TrueType font for best results
RichTextBox1.Se lFontName = "Arial"
RichTextBox1.Se lFontSize = 10

' Tell the RTF to base it's display off of the printer
LineWidth = WYSIWYG_RTF(Ric hTextBox1, 1440, 1440)
'1440 Twips=1 Inch

' Set the form width to match the line width
Me.Width = LineWidth + 200
End Sub

Private Sub Form_Resize()
' Position the RTF on form
RichTextBox1.Mo ve 100, 500, Me.ScaleWidth - 200, _
Me.ScaleHeight - 600
End Sub

Private Sub Command1_Click( )
' Print the contents of the RichTextBox with a one inch margin
PrintRTF RichTextBox1, 1440, 1440, 1440, 1440 ' 1440 Twips = 1
Inch
End Sub
=============== =============== =============== =============== =====

4. Insert a new standard module into the project, Module1.bas is
created by default.

5. Add the following code to Module1:
=============== =============== =============== =============== =====

Option Explicit

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

Private Const WM_USER As Long = &H400
Private Const EM_FORMATRANGE As Long = WM_USER + 57
Private Const EM_SETTARGETDEV ICE As Long = WM_USER + 72
Private Const PHYSICALOFFSETX As Long = 112
Private Const PHYSICALOFFSETY As Long = 113

Private Declare Function GetDeviceCaps Lib "gdi32" ( _
ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function SendMessage Lib "USER32" _
Alias "SendMessag eA" _
(ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, _
lp As Any) As Long
Private 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

''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''
'
' WYSIWYG_RTF - Sets an RTF control to display itself the same as
it
' would print on the default printer
'
' RTF - A RichTextBox control to set for WYSIWYG display.
'
' LeftMarginWidth - Width of desired left margin in twips
'
' RightMarginWidt h - Width of desired right margin in twips
'
' Returns - The length of a line on the printer in twips
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' '''''''''''
Public Function WYSIWYG_RTF(RTF As RichTextBox, _
LeftMarginWidth As Long, _
RightMarginWidt h As Long) As Long
Dim LeftOffset As Long, LeftMargin As Long, RightMargin As Long
Dim LineWidth As Long
Dim PrinterhDC As Long
Dim r As Long

' Start a print job to initialize printer object
Printer.Print Space(1)
Printer.ScaleMo de = vbTwips

' Get the offset to the printable area on the page in twips
LeftOffset = Printer.ScaleX( GetDeviceCaps(P rinter.hdc, _
PHYSICALOFFSETX ), vbPixels, vbTwips)

' Calculate the Left, and Right margins
LeftMargin = LeftMarginWidth - LeftOffset
RightMargin = (Printer.Width - RightMarginWidt h) - LeftOffset

' Calculate the line width
LineWidth = RightMargin - LeftMargin

' Create an hDC on the Printer pointed to by the Printer object
' This DC needs to remain for the RTF to keep up the WYSIWYG
display
PrinterhDC = CreateDC(Printe r.DriverName, Printer.DeviceN ame, 0,
0)

' Tell the RTF to base it's display off of the printer
' at the desired line width
r = SendMessage(RTF .hWnd, EM_SETTARGETDEV ICE, PrinterhDC, _
ByVal LineWidth)

' Abort the temporary print job used to get printer info
Printer.KillDoc

WYSIWYG_RTF = LineWidth
End Function

''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''
'
' PrintRTF - Prints the contents of a RichTextBox control using the
' provided margins
'
' RTF - A RichTextBox control to print
'
' LeftMarginWidth - Width of desired left margin in twips
'
' TopMarginHeight - Height of desired top margin in twips
'
' RightMarginWidt h - Width of desired right margin in twips
'
' BottomMarginHei ght - Height of desired bottom margin in twips
'
' Notes - If you are also using WYSIWYG_RTF() on the provided RTF
' parameter you should specify the same LeftMarginWidth and
' RightMarginWidt h that you used to call WYSIWYG_RTF()
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''
Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long, _
TopMarginHeight , RightMarginWidt h, BottomMarginHei ght)
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 NextCharPositio n As Long
Dim r As Long

' Start a print job to get a valid Printer.hDC
Printer.Print Space(1)
Printer.ScaleMo de = vbTwips

' Get the offsett to the printable area on the page in twips
LeftOffset = Printer.ScaleX( GetDeviceCaps(P rinter.hdc, _
PHYSICALOFFSETX ), vbPixels, vbTwips)
TopOffset = Printer.ScaleY( GetDeviceCaps(P rinter.hdc, _
PHYSICALOFFSETY ), vbPixels, vbTwips)

' Calculate the Left, Top, Right, and Bottom margins
LeftMargin = LeftMarginWidth - LeftOffset
TopMargin = TopMarginHeight - TopOffset
RightMargin = (Printer.Width - RightMarginWidt h) - LeftOffset
BottomMargin = (Printer.Height - BottomMarginHei ght) - TopOffset

' Set printable area rect
rcPage.Left = 0
rcPage.Top = 0
rcPage.Right = Printer.ScaleWi dth
rcPage.Bottom = Printer.ScaleHe ight

' 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
NextCharPositio n = SendMessage(RTF .hWnd, _
EM_FORMATRANGE, True, fr)
If NextCharPositio n >= TextLength Then Exit Do 'If done then
exit
fr.chrg.cpMin = NextCharPositio n ' 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

=============== =============== =============== =============== =====
6. Save the project.

7. Run the project.

8. Enter or paste some text into the RichTextBox.

9. Press the Print command button. Note that the printed output should
word-wrap at the same locations as displayed on the screen. Also, the
output should be printed with the specified one-inch margin all
around.
Nov 12 '05 #1
2 3200
It may be easier and more flexible to use Stephen Leban's rich text box for
Access rather than try to adapt pure VB code:
www.lebans.com

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"PC User" <pc*****@SoftHo me.net> wrote in message
news:25******** *************** ***@posting.goo gle.com...
I found some VB code to printout a richtext field and I'm trying to
adapt it to MS Access 2000. ...

Nov 12 '05 #2
"Allen Browne" <Al*********@Se eSig.Invalid> wrote in message news:<40******* *************** *@freenews.iine t.net.au>...
It may be easier and more flexible to use Stephen Leban's rich text box for
Access rather than try to adapt pure VB code:
www.lebans.com


Allen,

You may be right for most cases; however, my employer has an IT
section that pre-approves software for use on company machines and IT
security that remotely watches employee's computer activities. I've
requested software before and it has to go through a committee to be
approved. Without a more lengthly explaination, I would appreciate
someone with VB experience to help me.

Thanks for your reply,
PC
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3486
by: duikboot | last post by:
Hi all, I'm trying to export a view tables from a Oracle database to a Mysql database. I create insert statements (they look alright), but it all goes wrong when I try to execute them in Mysql, because the dates must have quotes on each side. I just don't know how make the dates right. Well I'll just show you the code and some insert statements it generates. Could anyone please help me?
1
1943
by: the_proud_family | last post by:
HELP ME PLEASE!! my email is the_proud_family@yahoo.com I can't get the ball to go up right side and then I need it to turn around and keep turning until velocity=0 I have been at it for the past 2 weeks now i give up and call for help. Please if anyone can gide me through i will be so grateful!! I have pasted my code below
7
3593
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title> </head> <style type="text/css">
5
2985
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig <<<<<<<<<<<<<<CODE>>>>>>>>>>>>>>>> <html>
23
3259
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
13
4316
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error "operation is not allowed when object is open" so I take out the line of code: BookDetails.Connection1.Open and it comes up with the error "operation is not allowed when object
2
1882
by: tuan_vandyk | last post by:
Hi I desperately need help with my project. Theoretically everything should work bu it just isn't. Please email me for a copy of the project's source code. It was made in Turbo C++ 5. Please if anyone can help me please email me at tuan_vandyk@yahoo.co.uk Thanks
1
2284
by: glenn123 | last post by:
Hi, i am just about out of time to produce a working jukebox which has to perform these functions: to play music files when a track is chosen from a list which when the user presses the change genre button the list is populated with a list of that genre. I have got the interface done to satisfaction, my problem is that when i press the change genre button nothing happens and when i select a track to play from the list which is setvisible and...
2
3805
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null c,null,D
2
2655
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null
0
8265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2625
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.