Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2009-04-10
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

722 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