Results 1 to 3 of 3

Thread: Mailing Labels using printer object

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Mailing Labels using printer object

    here is some simple code to print mailing labels from a database or any other input method, using the VB printer object

    it allows for you to set any size of label or qty of label to page, either in code or from user selection

    there is no provision in this code to make sure that the text will fit on the label, either in width of text or lines per label, you will need to limit user choices to what will fit and work, the textwidth here is controlled by the database field sizes, so i didn't need to worry about it

    the code is hacked out of much larger sub, so you will need to declare all your own variables etc. i have tried to comment everything to make it easy to use
    VB Code:
    1. 'change to the printer you want to use first
    2.               ' Make sure that the printer paper size is correct for the label sheets in use, otherwise you can spit out many sheets of labels, ie printer set for Letter, when the labels and code are for A4 size
    3.             With Printer
    4.                 .Font.Name = "Arial" 'optional setting printer fonts etc
    5.                 .Font.Size = 10
    6.             End With
    7.             'these options can be selected by user input from textbox or option buttons. design your own form
    8.             la = 3 'labels across page
    9.             ld = 15 'labels down page  
    10.             np = la * ld
    11.             labelw = 7 * 567 'width of label centimeters  for inches change 567 to 1440
    12.             ' if any vertical gap between labels add to label width
    13.             labelh = 2.5 * 567   ' height of label in cm for inches change 567 to 1440
    14.             ' if any horizantal spacing between labels add to label height
    15.             tmargin = 500 'top margin from top of page to first line printing position
    16.             lmargin = 600 ' left margin from left edge of page to first character
    17.             ' margins must be big enough for the selected printer to be able to print to
    18.             ' test the margins and adjust
    19.             ' if the page has ½" to edge of label, a starting value for top and left margins might be 900
    20.             ' if the label goes to the edge of the page then try about 250
    21.            
    22.         For i = 0 To DataRS.RecordCount - 1
    23.             If (i + np) Mod np + 1 = 1 And Not i = 0 Then Printer.NewPage
    24.  
    25.             lmarg = ((i + la) Mod la) * labelw + lmargin
    26.             topline = (((i + la * ld) Mod la * ld) \ la) * labelh + tmargin
    27.            
    28.             ' print each label from database records or whatever
    29.             With Printer
    30.                 .CurrentY = topline
    31.                 For j = 0 To DataRS.Fields.Count - 1
    32.                    'where DataRS is a recordset with the fields in the order to be printed
    33.                     .CurrentX = lmarg
    34.                     Printer.Print DataRS.Fields(j)
    35.                  Next
    36.             End With
    37.             DataRS.MoveNext
    38.  
    39.         Next
    40.    
    41.     Printer.EndDoc
    Last edited by westconn1; Jul 15th, 2009 at 04:29 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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