• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

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

0
JoshinHtown
Asked:
JoshinHtown
  • 4
  • 3
1 Solution
 
oobaylyCommented:
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.

0
 
JoshinHtownAuthor Commented:
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..
0
 
oobaylyCommented:
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.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
JoshinHtownAuthor Commented:
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 :(
0
 
oobaylyCommented:
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.
0
 
JoshinHtownAuthor Commented:
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?
0
 
oobaylyCommented:
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/
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now