Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Mail Merger, SQL , Asp.Net VB

am new to Mail Merge.

I have to build a mail merge application in ASP.net VB.
The end user will update a Mail merger document template, then the other end users can able to merge the database values with the uploaded document and can download the new merged doocument with data.

any help or link ?
Jini Jose
Jini Jose
3 Solutions
Which type of document are you trying to create a template for and how will it be mailed out? (or is it just to create a word doc which can be saved manually to the user's drive?)

We have something where a user provides a Word template, and other users can create saveable/printable documents with fields dragged into from the database. Is that what you're after?
here's the relevant part of our code, the Office 2003 dll's had to be installed on the web server first.
Dim oMissing As Object = System.Reflection.Missing.Value
            Dim oEndOfDoc As Object = "\endofdoc"

            'Start Word and create a new document.
            Dim oWord As Word._Application
            Dim oDoc As Word._Document
            oWord = New Word.Application()
            oWord.Visible = False
            Dim oTrue As Object = True

            Dim oTemplate As Object = "C:\Enablement.dot"
            oDoc = oWord.Documents.Add(oTemplate, oMissing, oMissing, oMissing)

            Dim oBookMark As Object = "Ref"
            oDoc.Bookmarks.Item(oBookMark).Range.Text = RefID

            Dim oBookMark1 As Object = "DateToday"
            oDoc.Bookmarks.Item(oBookMark1).Range.Text = ATPIssue.ToString

            Dim oBookMark2 As Object = "SHAapp"
            oDoc.Bookmarks.Item(oBookMark2).Range.Text = SHAApproval

            Dim oBookMark3 As Object = "ProductType"
            oDoc.Bookmarks.Item(oBookMark3).Range.Text = Product

            ' Save the document
            Dim newfileguid As Guid = Guid.NewGuid()
            Dim strnewFileName As String = newfileguid.ToString
            'objSubmission.Title) + ".doc";
            'Dim newFilename As Object = "C:\" & newfileguid & ".doc"

            Dim docfilename As String = "CertID" & ".doc"

            Dim newFilename As Object = "\\mypath\" & docfilename

            oDoc.SaveAs(newFilename, oMissing, oMissing, oMissing, oMissing, oMissing, _
             oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, _
             oMissing, oMissing, oMissing, oMissing)

            oDoc.Close(oTrue, oMissing, oMissing)
            oWord.Quit(oTrue, oMissing, oMissing)
           'Read and  Save to Database...
            Dim fs As New FileStream(newFilename.ToString(), FileMode.Open, FileAccess.Read)
            Dim fileData As Byte() = New Byte(fs.Length - 1) {}
            fs.Read(fileData, 0, System.Convert.ToInt32(fs.Length))

            Response.AddHeader("Content-disposition", "attachment; filename=" & docfilename)
            'Response.AddHeader("attachment; filename=" & Convert.ToString(newFilename))
            Response.ContentType = "application/vnd.ms-word"

Open in new window

Jini Jose.Net Team LeadAuthor Commented:

any user can upload a Mail Merge Template. that user or any other user in that company can then select that template. Then the user need to search for the records. after finding the records the user can merge the selected records to the selected mail merge document. The user then download the merged document.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Jini Jose.Net Team LeadAuthor Commented:
the selected records may have hundreds of records. each records should appear in seperate page of the new document.
In your provided example is it possible ?
ok, that sounds a little more complicated in that you'd need to have the code for uploading the template be able to map the merge fields to the database table's columns, then you should be able to handle the fields iteratively when it comes to printing them.
it really depends on what you're trying to deliver in the document, if it's large amounts of data, reporting services may be a better option for you
Jini Jose.Net Team LeadAuthor Commented:
it is just printing some address labels. The option is user can upload their desired format.
and for mapping the database fields are limited. Just like name, address, phone, postal code etc..
if the template has the page breaks set up correctly and the printer is loaded with labels, this should work fine; the formatting should be handled by the template rather than the data you're inserting.
Jini Jose.Net Team LeadAuthor Commented:
so the recordset having 10 records and if I use your code, it will generate 10 pages ?
the code given just creates a template for a single record, as it was designed for printing certificates for suppliers.

How are you creating the recordsets for each batch of labels? Could you explain the whole process and usage of your site please, if I know how someone is selecting a template and the recipients they want to mail, I might be able to give you a better response.
forgot to ask, when is the print run supposed to happen? Will the user just print them as required, or will the labels be queued/scheduled on a central printer?
Jini Jose.Net Team LeadAuthor Commented:
the user wants to take print out the whole address of the employees. but the format for the output is different depending upon the requirement.
so the user can upload a mail merge template.
then he search for the corresponding records. for eg:- the employees who resides in India.
then he select the available template to merge the data.
after that he click the generate then the system will generate the merged document.
and then show a link to download the merged document.

for eg; the user uploads the template with the following information, Name, Address , Postal code
then he search for the records. suppose he get 100 records.
then he select the template and click merge.
so the out put document should have 100 pages or 25 pages with 4 records each in a page. that depends upon the uploaded mail merge documenet.
Jini Jose.Net Team LeadAuthor Commented:
"forgot to ask, when is the print run supposed to happen? Will the user just print them as required, or will the labels be queued/scheduled on a central printer?"

when the user clicks on "merge document" or "print label" the system generates the document and will show the download option.
ok, so you're building a result set on the ASP page, with results present in a gridview or similar object, then some selection method for the labels (dropdown list or radiobutton list etc)...

Let me have a think on this one, I'll need to check the coding for templates as the label templates allow lists to be referenced.
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:
Personally, for simple mail merges, I don't bother using VB but I do it directly from SQL Server who can do mail merges failry easily
Jini Jose.Net Team LeadAuthor Commented:
hi all

thanks for your help

lastly i developed the code. once i got the time to write an article i will post it in detail.
Kumaraswamy RCommented:
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.

Featured Post

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.

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