How to create mailing labels or customized letters using MS Word MailMerge

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbsog7777
    New Member
    • Dec 2009
    • 1

    How to create mailing labels or customized letters using MS Word MailMerge

    I was trying to use the sample code below, but I encountered two errors:
    Application.DoE vents() and AutoText(entry) . I am not sure how to correct the errors. I trying to use the sample code to create a mailmerge routine that accepts data from my SQL database and create a word doc that ce be previewed in the client browser. My OS is XP Pro and I am coding in VB.NET.

    Protected Sub CreateWordMailM erge(ByVal strLabelName As String, _
    ByVal wd_AlignmentCod e As Word.WdCellVert icalAlignment, _
    ByVal intOffset As Single, _
    ByVal strFileName As String, _
    ByVal bUseTemplate As Boolean)

    Dim oApp As Word.Applicatio n
    Dim oDoc As Word.Document
    Dim x As Integer
    Dim bOK As Boolean = False
    Dim oAutoText As Word.AutoTextEn try

    Try
    'Start a new document in Word
    oApp = CType(CreateObj ect("Word.Appli cation"), Word.Applicatio n)
    If bUseTemplate Then
    Try
    oDoc = oApp.Documents. Open(CType(strL abelName, System.Object), False, True, _
    False, , , True, , , Word.WdOpenForm at.wdOpenFormat Document, , True)

    Catch MyException As System.Exceptio n
    MsgBox(String.C oncat("Unable to open ", strLabelName, _
    ControlChars.Cr .ToString, ControlChars.Cr .ToString, _
    "Error Message: ", MyException.Mes sage, ControlChars.Cr .ToString, _
    "Source: ", MyException.Sou rce, ControlChars.Cr .ToString, _
    "InnerException : ", MyException.Inn erException.Mes sage, ControlChars.Cr .ToString), _
    MsgBoxStyle.Cri tical, "Label Template Open Failure")
    CType(oApp, Word._Applicati on).Quit()

    oApp = Nothing

    Exit Sub

    End Try
    Else
    oDoc = oApp.Documents. Add

    End If

    oApp.Visible = True ' Make MS Word visible
    Application.DoE vents()

    ' Do MailMerge
    With oDoc.MailMerge
    If Not bUseTemplate Then
    With .Fields
    .Add(oApp.Selec tion.Range, "Line1")
    oApp.Selection. TypeParagraph()
    .Add(oApp.Selec tion.Range, "Line2")
    oApp.Selection. TypeParagraph()
    .Add(oApp.Selec tion.Range, "Line3")
    oApp.Selection. TypeParagraph()
    .Add(oApp.Selec tion.Range, "Line4")
    oApp.Selection. TypeParagraph()
    .Add(oApp.Selec tion.Range, "Line5")

    End With
    oAutoText = oApp.NormalTemp late.AutoTextEn tries.Add("word AutoTextTemp", oDoc.Content)
    oDoc.Content.De lete()

    End If ' If bUseTemplate

    ' Set up the mail merge type as mailing labels and use
    ' a tab-delimited text file as the data source.
    .MainDocumentTy pe = WdMailMergeMain DocType.wdFormL etters
    .OpenDataSource (strFileName, Word.WdOpenForm at.wdOpenFormat Text, False, True, False, False)

    If Not bUseTemplate Then
    '
    'Create the new document for the labels using the
    AutoText(entry)
    '
    oApp.MailingLab el.CreateNewDoc ument(CType(str LabelName, String), "", "wordAutoTextTe mp", False, Word.WdPaperTra y.wdPrinterManu alFeed)

    End If ' If bUseTemplate
    '
    'Execute the mail merge to generate the labels.
    .Destination = WdMailMergeDest ination.wdSendT oNewDocument
    .Execute()
    '
    If Not bUseTemplate Then
    'Delete the AutoText entry
    oAutoText.Delet e()

    End If ' If bUseTemplate

    End With
    '
    'Close the original document so that
    'the Mail Merge results are displayed
    CType(oDoc, Word._Document) .Close(False)
    '
    If Not bUseTemplate Then

    ' Apply vertical alignment
    For x = 1 To oApp.ActiveDocu ment.Tables.Cou nt
    oApp.ActiveDocu ment.Tables.Ite m(x).Range.Cell s.VerticalAlign ment = wd_AlignmentCod e

    Next

    Application.DoE vents()

    For x = 1 To oApp.ActiveDocu ment.Tables.Cou nt
    oApp.ActiveDocu ment.Tables.Ite m(x).LeftPaddin g = oApp.InchesToPo ints(intOffset)
    oApp.ActiveDocu ment.Tables.Ite m(x).Rows.LeftI ndent = oApp.InchesToPo ints(intOffset)

    Next

    Application.DoE vents()

    End If

    If oApp.ActiveWind ow.View.Type <> Word.WdViewType .wdPrintView Then
    If oApp.ActiveWind ow.View.SplitSp ecial = Word.WdSpecialP ane.wdPaneNone Then
    oApp.ActiveWind ow.ActivePane.V iew.Type = WdViewType.wdPr intPreview

    Else
    oApp.ActiveWind ow.View.Type = WdViewType.wdPr intPreview

    End If

    End If
    '
    'Prevent save to Normal template when user exits Word
    oApp.NormalTemp late.Saved = True
    oApp = Nothing
    oDoc = Nothing

    Catch MyException As System.Runtime. InteropServices .COMException
    MsgBox(String.C oncat("Error occured while communicating with MS Word(", ")) ControlChars.Cr .ToString, ControlChars.Cr .ToString, ErrorCode: ", MyException.Err orCode, ControlChars.Cr .ToString, "Error Message: ", MyException.Mes sage, ControlChars.Cr .ToString, "Source: ", MyException.Sou rce, ControlChars.Cr .ToString, "StackTrace : ", MyException.Sta ckTrace, ControlChars.Cr .ToString), MsgBoxStyle.Cri tical, "MS Word Problem")

    End Try

    End Sub
Working...