How can I make these Mailing Labels in VB.NET???

Posted on 2009-04-10
Last Modified: 2012-05-06
Hello All-

Through my research on the web, I have found code that takes a Datatable, converts it to a CSV, and then creates mailing labels using Microsoft Word.  It has come to my attention that there are now licensing issues if I wanted to allow end users access to this tool.  My question is does anyone have any code to use Open Office or another free product that will create these same mailing labels from a datatable and produce a RTF file??

I'm offering maximum points because I'm in dire need of this and would greatly appreciate code.

Thank you all...

Dim dgv As Data.DataTable = DirectCast(Session("StoreGrid"), Data.DataTable)

        '++++++++++++++++++++++ try to convert datatable to csv

        Dim sw As New System.IO.StreamWriter("C:\LabelsFile.csv", False)

        Dim icolcount As Integer = dgv.Columns.Count

        For i As Integer = 0 To icolcount - 1


            If i < icolcount - 1 Then


            End If



        For Each drow As Data.DataRow In dgv.Rows

            For i As Integer = 0 To icolcount - 1

                If Not Convert.IsDBNull(drow(i)) Then


                End If

                If i < icolcount - 1 Then


                End If





        '++++++++++++++++++++++++++ end convert to csv

        'Dim t As Type = System.Type.GetTypeFromProgID("Word.Application", "localhost")

        'Dim oApp As Application = DirectCast(System.Activator.CreateInstance(t), ApplicationClass)

        '++++++++++++ for word

        Dim oApp As Microsoft.Office.Interop.Word.Application

        Dim oDoc As Microsoft.Office.Interop.Word.Document 'Word.Document

        'Start a new document in Word

        oApp = CreateObject("Word.Application")

        oDoc = oApp.Documents.Add

        With oDoc.MailMerge

            'Insert the mail merge fields temporarily so that

            'you can use the range that contains the merge fields as a layout

            'for your labels -- to use this as a layout, you can add it

            'as an AutoText entry.

            With .Fields

                .Add(oApp.Selection.Range, "owner_name")


                .Add(oApp.Selection.Range, "address")


                .Add(oApp.Selection.Range, "Mail_City")

                oApp.Selection.TypeText(",  ")

                .Add(oApp.Selection.Range, "Mail_State")

                oApp.Selection.TypeText("  ")

                .Add(oApp.Selection.Range, "Mail_Zip")

            End With

            Dim oAutoText As Microsoft.Office.Interop.Word.AutoTextEntry 'Word.AutoTextEntry

            oAutoText = oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout", oDoc.Content)

            oDoc.Content.Delete() 'Merge fields in document no longer needed now

            'that the AutoText entry for the label layout

            'has been added so delete it.

            'Set up the mail merge type as mailing labels and use

            'a tab-delimited text file as the data source.

            .MainDocumentType = WdMailMergeMainDocType.wdMailingLabels 'wdMailingLabels

            .OpenDataSource(Name:="C:\LabelsFile.csv") 'Specify the data source here

            'Create the new document for the labels using the AutoText entry

            'you added -- 5160 is the label number to use for this sample.

            'You can specify the label number you want to use for the output

            'in the Name argument.

            oApp.MailingLabel.CreateNewDocument(Name:="5160", Address:="", _

                AutoText:="MyLabelLayout") 'LaserTray:=wdPrinterManualFeed)

            'Execute the mail merge to generate the labels.

            .Destination = WdMailMergeDestination.wdSendToNewDocument 'wdSendToNewDocument


            'Delete the AutoText entry you added


        End With

Open in new window

Question by:JoshinHtown
  • 4
  • 3
LVL 15

Expert Comment

ID: 24116194
I wouldn't recommend using RTF as the spec (if you can call it that) is pretty loose. The two options I'd take are to create a PDF, or a simple bitmap that can be returned by the web server (I presume it's a web app from the use of the Session object).

Creating a PDF is pretty easy using iTextSharp or Siberix. We use the latter as some of our solutions can't use GPL'd code. Also it was the only one we could find 5 years ago.


Author Comment

ID: 24116243
Hello oobayly-  Thank you for the fast response.  Unfortunately I'm looking for a Free product like Open Office or something similar to create the mailing labels and then export a document of some sort.  Does anyone know how to use Open Office to create mailing labels through VB.NET??

LVL 15

Accepted Solution

oobayly earned 500 total points
ID: 24117212
Funny, we use solely OpenOffice in our office, but I've never used it to generate documents in this way, so I'm afraid I'm not going to be much help.
Whenever we've had to generate documents automagically, I've found it to be just as simple to generate a PDF, rather than interfacing with a 3rd party app. If anything it also makes life easier if you have to move the code to another server. Just copy across your libs and jobs a good 'un.

The reason I'm advocating PDF is that it's a good solution for end users as it's not platform specific, and they can be secured easily against editing.

Regards it being free, iTextSharp is GPL, so unless you're distributing a closed source app, it shouldn't cost you anything.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


Author Comment

ID: 24117312
Yeah the only issue with creating a PDF out of the mailing labels is when the end user tries "printing" the labels.  There won't always be consistency in the printer margins, etc.  At any rate, if I wanted to try to make my labels into a PDF, I would still need to first create them in Open Office or some software before generating the PDF right?  Still looking for a viable solution :(
LVL 15

Expert Comment

ID: 24117521
Fait point about the margins, pdf readers do have a habit of fitting the full pdf area into the printable bounds. Regarding generating the PDF, you would actually generate the PDF entitely in your app. You can get some very smart results, but the intial setup does take a bit more time than using a WYSIWYG editor.
Anyhow, I can see you're not convinced about using PDF :-), I hope you find your desired solution.

Author Comment

ID: 24117541
I think I would like to try the PDF solution actually.  Do you have any code in VB or know where to point me to get me started on creating mailing labels and producing a PDF of them?
LVL 15

Expert Comment

ID: 24119200
Well, the following page appears to be an abridged version of the book on how to use iText.

Unfortunately it's for the Java version, so while this would be fine if you're using C#, as you're using VB.Net it would mean that you'd have to "translate" the syntax. In reality it's not to difficult as you may well be aware. The only other problem may be that Java IO classes are a bit different, but as long as you know how they work in .Net, again it's not a massive issue.

As Java & C# are syntacticly (is that a word) very similar, you could try copy the Java code into the following site (translates C# to VB)

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 84
Word Directory is not in the drop down list 4 24
Visual Studio 2015 Source Code Control 10 29
SQL Exceptions 3 36
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

914 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

23 Experts available now in Live!

Get 1:1 Help Now