mail merge of letter format

Discussion in 'C#' started by akpaga, May 22, 2008.

  1. #1
    hi i am using the below code and it works fine and prints the names of the members in form of labels.

    But what i exactly want is it should open a existing word template
    and insert the names at the place where it is mentioned Name:
    and for each new name it should be on a new page --- kind of mail merge to print letters
    please help.

    thanks in advance
    ---------code---------
    <%@ Language=VBScript %>
    <HTML>
    <BODY>
    <SCRIPT LANGUAGE=VBScript>
    Sub CreateDataDoc(oApp)
    ' Declare variables.
    Dim sServer,oDoc,oRS,sTemp,sHead,oRange,oField

    ' Place your server's name here.
    sServer = "localhost"
    ' Create a new document.
    Set oDoc = oApp.Documents.add
    ' Create a new recordset.
    Set oRS = CreateObject("ADODB.Recordset")
    ' Open the XML recordset from the server
    oRS.Open "http://localhost/WordDoc/getdata.asp"
    ' Convert the recordset to a string.
    sTemp = oRS.GetString(2, -1, vbTab) ' 2 = adClipString

    ' Append the field names to the front of the string.
    For Each oField In oRS.Fields
    sHead = sHead & oField.Name & vbTab
    Next

    ' Strip off the last tab.
    sTemp = Mid(sHead, 1, Len(sHead) - 1) & vbCrLf & sTemp

    ' Get a range object and insert the text into the document.
    Set oRange = oDoc.Range
    oRange.Text = sTemp

    ' Convert the text to a table.
    oRange.ConvertToTable vbTab
    ' Save the document to a temp file.
    oDoc.SaveAs "C:\data.doc"
    ' Close the document (no save).
    oDoc.Close False
    End Sub


    Sub ButtonClick()
    Dim oApp
    Dim oDoc
    Dim oMergedDoc

    ' Create an instance of Word.
    Set oApp = CreateObject("Word.Application")

    ' Create our data file.
    CreateDataDoc oApp

    ' Add a new document.
    Set oDoc = oApp.Documents.Add
    With oDoc.MailMerge
    ' Add our fields.
    .Fields.Add oApp.Selection.Range, "member_name1"
    oApp.Selection.TypeText " "
    .Fields.Add oApp.Selection.Range, "member_name2"
    oApp.Selection.TypeText " "

    ' -----------------------------------------------------
    ' Create an autotext entry.
    Dim oAutoText
    Set oAutoText = oApp.NormalTemplate.AutoTextEntries.Add _
    ("MyLabelLayout", oDoc.Content)
    oDoc.Content.Delete
    .MainDocumentType = 1 ' 1 = wdmailinglabels

    ' Open the saved data source.
    .OpenDataSource "C:\data.doc"
    '-------------------------------------------------------------------
    ' Create a new document.
    oApp.MailingLabel.CreateNewDocument "5160","", _
    "MyLabelLayout", , 4 ' 4 = wdPrinterManualFeed

    .Destination =wdSendToPrinter ' 0 = wdSendToNewDocument
    ' Execute the mail merge.
    .Execute

    oAutoText.Delete
    End With

    ' Close the mail merge edit document.
    oDoc.close False
    ' Get the current document.
    Set oMergedDoc = oApp.ActiveDocument
    ' Show Word to the user.
    oApp.Visible = True

    ' Uncomment these lines to save the merged document locally.
    oMergedDoc.SaveAs "C:\test.doc"
    oMergedDoc.Close False
    oApp.Quit False
    End Sub
    </SCRIPT>
    <INPUT type=button value="Create Word Document" onClick="vbscript:ButtonClick">
    </BODY>
    </HTML>
     
    akpaga, May 22, 2008 IP
  2. iconico

    iconico Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sounds easy, just have your template sitting there, make a copy of it and then open it, search through the text for yout tokens and save it. 4GuysFromRolla.com probably have an example.
     
    iconico, Jun 10, 2008 IP