Question Print Labels

gate7cy

Well-known member
Joined
May 11, 2009
Messages
119
Programming Experience
3-5
Hello to all. I am using VS 08 together with MySQL for the database.
I am trying to print labels using an application I am developing. I found this template in word in which represents the label paper I have. I have attached the template. The template consists of a table with 13 rows and 5 columns. In every cell in the table I am adding bookmarks so I can send data from vb. I have used bookmarks before with no problems. In this case the data do not go in the correct place. I am guessing because they are within a cell in the table. So I will have to choose the cell before sending the data. I do not know how to do that. What I have tried is this:

VB.NET:
 Dim oWord As Word.Application
        Dim oDoc As Word.Document


        'Start Word and Open document template

        oWord = CType(CreateObject("Word.Application"), Word.Application)
        oWord.Visible = False ' tells whether to show the Office Word program or not


        oDoc = oWord.Documents.Add("C:\Users\KoRnHoLiOs\Documents\Visual Studio 2008\Projects\Shop\pok.doc")

        oDoc.Tables(1).Cell(2, 1).Range.Bookmarks.Add("name").Range.Text = reader("name").ToString

               oDoc.Bookmarks.Add("price").Range.Text = reader("sell").ToString
        oDoc.Bookmarks.Add("colour").Range.Text = reader("colour").ToString
        oDoc.Bookmarks.Add("barcode").Range.Text = reader("barcode").ToString
        oWord.PrintOut()

        oWord.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
        oWord.Quit()
        oWord = Nothing
        reader.Close()
        conn.Dispose()


You can see I attempted to select the cell I want to print in but with no results. The rest of the bookmarks just send data with no cell selected. Also in this case the text is printed elsewhere. Anybody with some help on how to select the cell I want to send data? As shown in the code one of the bookmarks is a barcode. I am senting the number sequence which I want word to use the barcode font and convert to a barcode. This does not work. I place the bookmark on the the highlighed barcode font but nothing. I have tried to use Crystal Reports to print my labels but I am having problems just connecting to my database as I am using Mysql. Anyone has any links to assist me in printing this labels. Any solution accepted.m Whether that is a finished component I can download (even one that needs paying it), suggestions, links, tutorials.


Thanks for all your efforts and replies.
 
Daft question time ;) Why arent you just printing straight from your VB application - ie dont bother with Word or Crystal Reports?

What barcode methodology are you using?
 
For barcode I am using datetime together with item names to generate unique, one off barcodes. How to print directly from VB? I have to program margins, orientation, where to place what? I was never good in using the .paint of VB. I could not print a single form and remove the huge margins. I can put my head on how to print labels?

Please advise and thanks for you reply
 
Here's something to start you in the right direction.

VB.NET:
Option Explicit On
Option Strict On

Imports System.Drawing.Printing

Public Class Form1

    Public WithEvents pdPrinter As New PrintDocument

    Private Sub TestPrintPreview(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

        Using ppvwPrinter As New PrintPreviewDialog
            With ppvwPrinter
                .Document = pdPrinter
                .Width = Screen.PrimaryScreen.Bounds.Width
                .Height = Screen.PrimaryScreen.Bounds.Height
                .PrintPreviewControl.Zoom = 0.66
                .ShowDialog()
            End With
        End Using
    End Sub

    Private RecordNumber As Integer = 0
    Private PageNumber As Integer = 0
    Private TotalPages As Integer = 1

    Private fArial10Bold As New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)
    Private brBlack As New SolidBrush(Color.Black)
    Private sfMiddleCenter As New StringFormat


    Private Sub Begin_Printing(ByVal sender As System.Object, _
    ByVal e As System.Drawing.Printing.PrintEventArgs) _
     Handles pdPrinter.BeginPrint
        sfMiddleCenter.Alignment = StringAlignment.Center
        sfMiddleCenter.LineAlignment = StringAlignment.Center

        pdPrinter.DocumentName = "Test Labels"
        pdPrinter.PrintController = New Printing.StandardPrintController()
        RecordNumber = 0
        PageNumber = 1
        TotalPages = 2
    End Sub

    Private Sub Test_PrintPage(ByVal sender As System.Object, _
    ByVal e As PrintPageEventArgs) Handles pdPrinter.PrintPage
        With e.Graphics
            .PageUnit = GraphicsUnit.Millimeter

            For i As Integer = 1 To 5
                For j As Integer = 1 To 13
                    RecordNumber += 1
                    .ResetTransform()
                    .TranslateTransform(i * 30, j * 20)
                    Using pBlack As New Pen(Color.Black, 0.5)
                        .DrawRectangle(pBlack, 1, 1, 30 - 1 - 1, 20 - 1 - 1)
                    End Using
                    .DrawString(RecordNumber.ToString, fArial10Bold, brBlack, _
                    30 / 2, 20 / 2, sfMiddleCenter)
                Next j
            Next i
        End With

        PageNumber += 1
        If PageNumber <= TotalPages Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub

End Class
 
Back
Top