Printing Problem

Posted on 2003-11-04
Last Modified: 2010-05-03
Hi all, In the following I need to do two things:

Function PrintPage()

    Dim i As Integer ' variable to call the textbox to print
    With Printer
        .Font = LBL1.Font
        .FontSize = LBL1.FontSize
        .FontBold = LBL1.FontBold
        Printer.Print LBL1.Caption
        Printer.Print LBL7.Caption
        Printer.Print TXT6.Text
        Printer.Print LBL2.Caption
        Printer.Print TXT1.Text
        Printer.Print LBL3.Caption
        Printer.Print TXT2.Text
        Printer.Print LBL4.Caption
        Printer.Print TXT3.Text
        Printer.Print LBL5.Caption
        Printer.Print TXT4.Text
        Printer.Print LBL6.Caption
        Printer.Print TXT5.Text
        Printer.Print LBL8.Caption
        Printer.Print TXT8.Text
        Printer.Print LBL10.Caption
        Printer.Print TXT9.Text
        Printer.Print LBL11.Caption
        Printer.Print TXT10.Text
        Printer.Print LBL12.Caption
        Printer.Print TXT11.Text
        Printer.Print LBL13.Caption
        Printer.Print TXT12.Text
        Printer.Print TXT7.Text
        Printer.Print LBL9.Caption
    End With
End Function

1. How do i print the above with a space in between each item ?

2. How would one print an image in "picture1" ? The picture box is not in the above but I would like to add it!

Any other ideas such as centering the text etc would be welcome :)


Question by:Canon

Accepted Solution

NBrownoh earned 200 total points
ID: 9684641
ok to add a space after each just add & vbcrlf to the end of each line you have
        Printer.Print LBL7.Caption & VbCrlf
        Printer.Print TXT6.Text & VbCrlf
        Printer.Print LBL2.Caption & VbCrlf
        Printer.Print TXT1.Text & VbCrlf
        Printer.Print LBL3.Caption & VbCrlf

to print a picture you do this:

        Printer.PaintPicture Picture1.Image, 0, 0,

that will put the picture at 0 twips to the left and 0 twips from the top, you can also set the size of the image if you wanted to.

         Printer.PaintPicture Clipboard.GetData, 0, 0, lWidth, lHeight

settings lWidth and lHeight to the twips that you want it to be.

LVL 17

Expert Comment

ID: 9684980
I can help you big-time here...back soon.

Expert Comment

ID: 9684994
oops on that clipboard.getdata part, change that to yourpicturebox.image yourpicturebox being the picture box that has the image you want to print in it
LVL 17

Expert Comment

ID: 9685100
Read the floowing is summarises what can be done with the printer.

In simple terns never print directly to the printer as it takes a lot of paper to debug print.  Create a form or use a picture box on a form where you can print.

When Typing Code
Dim OutP as Printer

When Debugging....
Dim OutP as Object
Set Outp = frmDummy

When Debugged....
Dim OutP as Object
Set Outp = Printer

So you can do things like this:

LineHeight = Outp.TexHeight + Twips *.01 ' line height plus a small gap

Outp.CurrentX = Twips * 2 ' 2 inches from left
Outp.CurrentY = Twips * 1 ' 1 inch from top
OutP.Print "YourText";

' move to next tab point
Outp.CurrentX = Twips * 4 ' 4 inches from left
OutP.Print "Your Next Bit";

' move down a line
Outp.CurrentY = Outp.CurrentY + LineHeight

Hope this helps:~)

------------Printing advise......
In simple terms you use the Printer Object like this:

Const Imperial = 1440 ' inches
Const Metric = 567 ' metric

Dim Twips as single
Twips = Imperial ' or Metric for centimeters
Printer.CurrentX = 1.5 * Twips ' 1.5 inches from left
Printer.CurrentY = 2 * Twips ' 2 inches from top
Printer.Print Text1; ' print some text

' tab over to next column
' use Format to handle numerics and dates, booleans, etc.
Printer.CurrentX = 3 * Twips ' 3 inches from left
Printer.Print Format(NumericValue,"0.00");

Printer.NewPage ' makes a new page
Printer.EndDoc  ' the document is complete

Here is a usage summary of how to handle the printer:

It is not hard to use VB's printer object. Here are some more samples using the VB Printer object or
a picture box for print preview.

When you realy get some subroutines together it is quicker to create your report in VB than either Access
or Data  reports.

After each ============== is a handy code section showing an example.

You can print anywhere you like on your page within the following positions:

l = Printer.ScaleLeft ' you must print to the right of here
t = Printer.ScaleTop ' below here
h = Printer.ScaleHeight ' above here
w = Printer.ScaleWidth ' left of here

' output to picture box or printer
If preview Then
  Set outp = picMyPictureControl
  outp.Height = Printer.Height
  outp.Width = Printer.Width
  Set outp = Printer
End If

' Change font
outp.Font.Name = "Arial"
outp.Font.Size = 8

' set line height to height of fields + a littlebit more
lineheight = outp.TextHeight("X") * 1.01

' set a factor so that you can think in either inches or centimeters
twips = 1440 ' or 567 for centimeters

'set margins
leftmargin = twips * 0.5 ' Set .5 inches from left

' set topmargin
topmargin = twips * 2 ' Set 2 inches from top
if Not Preview then
  if topmargin < t then
    topmargin = t
  end if
end if
' set bottom point
bottompoint = outp.ScaleHeight - lineheight - twips * 1

' set mid pint
centerpoint = (outp.ScaleWidth - outp.ScaleLeft) * 0.5 + outp.ScaleLeft

' set rightmargin
rightmargin = outp.ScaleWidth - twips * 0.5

' Now do some printing
' move to printing position
outp.CurrentX = leftmargin
outp.CurrentY = topmargin

outp.Print "Print some text";

' print next field
outp.CurrentX = leftmargin + 2 * twips ' tab over 2 inches
outp.Print "Print some more";

' right justify to right side
outp.CurrentX = outp.ScaleWidth - outp.TextWidth(YourText$)
outp.Print YourText$;

' center justify
outp.CurrentX = centerpoint - outp.TextWidth(YourText$) * 0.5
outp.Print YourText$;

' tab to  2 inches from the left
Printer.CurrentX = 2 * twips

outp.Print "Print some more";

' Advance to next line
outp.CurrentY = outp.CurrentY + lineheight

' draw a line across
outp.line (leftmargin,outp.currenty)-(rightmargin-outp.currenty),0,bf

' draw a box arround an area
' you can also handle shading here as well rgb(250,250,250)but you
' need to use a windows API call to print on top of shading
' due to bug in windows
outp.line (leftmargin,topmargin)-(rightmargin-bottompoint),rgb(0,0,0),B

' Have we reached end of page or is this the top of the first page

Gosub CheckPage

If Page=0 or outp.CurrentY > bottompoint Then

  Page = Page + 1
  if page>1 Then ' if first page don't to end of page  
      ' print bottom of page stuff here
      outp.CurrentY = bottompoint - twips * 0.5
      outp.CurrentX = centerpoint
      outp.Print "Page " + CStr(Page);
      If preview Then
          ok = MsgBox("Continue Y/N", vbYesNo + _ vbQuestion, "End of Page")
          If ok <> vbYes Then
               ' unload your preview form
               Exit Sub
          end if
          outp.CurrentY = topmargin
      End If
  End If
  ' handle page headings in a gosub or soubroutine here
  gosub PrintHeadings
End If

' Graphics
' picG is the image you wish to print
' Both picG.Image and picG.Pictue can be used as a source

' S=Source of picture

' so you can crop the source picure like
sx =  cropleft ' Source x position
sy =  croptop
sw =  picG.ScaleWidth - cropright
sh = picG.ScaleHeight - cropbottom

' D=Destination pos X,Y and H & W is height and width
' e.g. DX=destination x postion

DX = t
DY = l
DW = r-l

' keep same aspect ratio of pircure width/height relationship

ARatio=picG.scaleheight/picg.scalewidth ' aspect ratio of source picture

' because  dh / dw = sh / sw = ARatio
' you can find the correct dh from dw
DH = DW * ARatio

' the raster op allows pictures to be merged, or just plain copied type "RasterOpConstants." to see
list of options

RasterOp = RasterOpConstants.vbSrcCopy

' now print the graphic
outp.PaintPicture picG.Picture, DX, DY, DW, DH, SX, SY, SW, SH, RasterOp

' you can create effects like upside down and mirror using -DH and -DW

' Autoshrink

Before you print your graphic you can test to see if it fits on the page, if it is too large then shrink
it a little so it fits.

Imagine you want to print at height position hp and left poisition lp you graphic is in picture box

' set position where graphic is to be printed.
DX = lp ' set desintation x
DY = hp ' set desintation y
DW = picG.Width
DH = picG.Height

' now here is the cute bit to stop graphic going over page.

If DH + DY > h Then ' where h = printer.scaleheight
  DH = h - DY ' set the height to the remaining height
  DW = DW * (DH / picG.Height) ' reduce width by same propotion
End If

Printer.PaintPicture picG.Picture, DX, DY, DW, DH

' Finishing up

Page = Page + 1

' print bottom of page stuff here
' best done in another subroutine
outp.CurrentY = bottompoint - twips * 0.5
outp.CurrentX = centerpoint
outp.Print "Page " + CStr(Page)

If preview Then
  ok = MsgBox("Complete", vbExclamation, "End of Document")
End If

' to abandon printing
If Not preview Then
End If

' Shading and transparent fonts

' First print a box
outp.Line (LeftM, startplace)-(RightM, endplace), RGB(247, 247, 247), BF
outp.FillStyle = 1 ' this should work in NT but not in 95
outp.FontTransparent = True
If Not preview Then ' fix windows 95 bug
  iBKMode = SetBkMode(outp.hdc, TRANSPARENT)
end if

' You need this in a code module
Public Declare Function SetBkMode Lib "gdi32" _
    (ByVal hdc As Long, ByVal nBkMode As Long) As Long

Public iBKMode As Long
Public Const TRANSPARENT = 1
Public Const OPAQUE = 2

' Zooming

Set a zoom factor and multiply all coordinates by the zoom factor. Examples:

ZF = 1 ' 1.5=150%, 2=200% etc.

outp.CurrentX = MyXPos * ZF
outp.CurrentY = MyYPos * ZF
outp.Font.Size = 8 * ZF

outp.Line (X1 * ZF, Y1 * ZF)-(X2 * ZF, Y2 * ZF),RGB(0,0,0),B

If preview Then
  Set outp = picMyPictureControl
  outp.Height = Printer.Height * ZF
  outp.Width = Printer.Width * ZF

How to select a printer:

What you do is create a setup program that saves the device name for the required printer.
Say you need 2 prinets one for documents and one for reports. You need as setup program
so operator can choose which one is needed.

Your setup from will have 2 combo boxes

so in the form load you need:

dim pr as printer
dim documents$
dim reports$



for each pr in printers
  comboDocuments.Additem pr.DeviceName
  comboReports.Additem Pr.Devicename

combodocuemnts=documents ' set the current name in the combo box
comboreports=reports ' set the current name in the combo box


In the save button on the form:
SaveSetting "MyProg", "Printers", "Reports", comboReports
SaveSetting "MyProg", "Printers", "Documents", comboDocuments

When you want to select a printer in your code you say:

' save the current printer name
Ok=SelectPrinterTypeOK("Reports") ' or "Documents"
if not ok then
  frmSelectPrinters.Show ' they need to choose a printer
  exit sub
end if

Just start printing

Printer.Print "ABCD"


And now the bits that do the work:

Function SelectPrinterTypeOK(PrinterType as String) as Boolean

' find the device name for the required printer
' then select that printer

Dim PName$
PName$ = getsetting("MyProg","Printers",PrinterType,"")
if len(Pname$)=0 then
  Exit Function
end if

End Functon

Function SelectPrinterNameOK(PrinterName as String) as Boolean

' select a printer by device name

Dim Pr as Printer

' search for the required printer
For each Pr in Printers
   If Ucase(Pr.DeviceName)=Ucase(PrinterName) Then
       Set Printer = Pr
       SelectPrinterNameOK = True
       Exit Function
   End If

End Function

If you want your app to be linkes to the current Windows printer.
In your App startup you need to set:

Printer.TrackDefault = True

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

LVL 76

Expert Comment

ID: 9685227
Just a thought for next time
You could have made your Labels as Textboxes into matching arrays.
So that you could have a loop

for i = 0 to TopIndex
        Printer.Print MyLabel(i).Caption & " " &   Printer.Print MyText(i)Text
Next i

Or you could name them in sequence

for i = 0 to TopIndex
       Printer.Print MyForm.Controls("MyLabel" & i).Caption & " " &   Printer.Print MyForm.Controls("MyText" & i).Text
Next i


Expert Comment

ID: 9686595

Author Comment

ID: 9687351
Hello All,

   Thanks to all who replied! I accepted the first answer because I just needed the two questions answered. Look for my next question regarding "transparent background" !


Expert Comment

ID: 9688226
cool, glad i could help

Expert Comment

ID: 10052113
Hi All

I am having problem With Printing TEXT Box itself

How can i print TextBox itself, I want print TextBox, radion button, checkbox?

Does any one know?

for ex.
printer.print "Enter your  Name"
printer.print "Here i want print textbox itself not for their caption or text"

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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

15 Experts available now in Live!

Get 1:1 Help Now