Solved

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

Posted on 2009-04-10
7
395 Views
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...

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
Comment
Question by:JoshinHtown
  • 4
  • 3
7 Comments
 
LVL 15

Expert Comment

by:oobayly
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.

0
 

Author Comment

by:JoshinHtown
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??

Thanks..
0
 
LVL 15

Accepted Solution

by:
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.
0
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

by:JoshinHtown
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 :(
0
 
LVL 15

Expert Comment

by:oobayly
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.
0
 

Author Comment

by:JoshinHtown
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?
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24119200
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries email authority 6 57
How to Convert a Quote to an Order in SalesForce 2 44
Database Connections Not being returned to Connection Pool 7 27
Query Syntax 17 36
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

803 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