Link to home
Start Free TrialLog in
Avatar of JoshinHtown
JoshinHtownFlag for United States of America

asked on

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

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...

Regards,
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
            sw.Write(dgv.Columns(i))
            If i < icolcount - 1 Then
                sw.Write(",")
            End If
        Next
        sw.Write(sw.NewLine)
        For Each drow As Data.DataRow In dgv.Rows
            For i As Integer = 0 To icolcount - 1
                If Not Convert.IsDBNull(drow(i)) Then
                    sw.Write(drow(i).ToString())
                End If
                If i < icolcount - 1 Then
                    sw.Write(",")
                End If
            Next
            sw.Write(sw.NewLine)
        Next
        sw.Close()
 
        '++++++++++++++++++++++++++ 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")
                oApp.Selection.TypeParagraph()
                .Add(oApp.Selection.Range, "address")
                oApp.Selection.TypeParagraph()
                .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
            .Execute()
 
            'Delete the AutoText entry you added
            oAutoText.Delete()
 
        End With

Open in new window

Avatar of oobayly
oobayly
Flag of United Kingdom of Great Britain and Northern Ireland image

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.

Avatar of JoshinHtown

ASKER

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??

Thanks..
ASKER CERTIFIED SOLUTION
Avatar of oobayly
oobayly
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 :(
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.
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?
Well, the following page appears to be an abridged version of the book on how to use iText.
http://itextdocs.lowagie.com/tutorial/

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)
http://www.developerfusion.com/tools/convert/csharp-to-vb/