MailMerge CreateDataSource方法(Microsoft Word)VB.NET

时间:2017-03-26 13:23:47

标签: vb.net email merge ms-word

我在VB.NET中创建HeaderRecord时遇到问题。这些条目写成一行而没有分隔(HeaderRecord:=" Name,Address,City,State,Zip")因此它会出错。

Word 2016 Image

Error Image

Private Sub FillRow(ByVal Doc As Word.Document, ByVal Row As Integer, _
                    ByVal Text1 As String, ByVal Text2 As String, _
                    ByVal Text3 As String, ByVal Text4 As String)

    With Doc.Tables.Item(1)
        ' Insert the data in the specific cell.
        .Cell(Row, 1).Range.InsertAfter(Text1)
        .Cell(Row, 2).Range.InsertAfter(Text2)
        .Cell(Row, 3).Range.InsertAfter(Text3)
        .Cell(Row, 4).Range.InsertAfter(Text4)
    End With
End Sub

Private Sub CreateMailMergeDataFile()
    Dim wrdDataDoc As Word._Document
    Dim iCount As Integer

    ' Create a data source at C:\Users\fweir\Desktop\test\DataDoc.doc containing the field data.
    wrdDoc.MailMerge.CreateDataSource(Name:="C:\Users\fweir\Desktop\test\DataDoc.doc", HeaderRecord:="Name,Address,City,State,Zip")

    ' Open the file to insert data.
    wrdDataDoc = wrdApp.Documents.Open("C:\Users\fweir\Desktop\test\DataDoc.doc")
    For iCount = 1 To 2
        wrdDataDoc.Tables.Item(1).Rows.Add()
    Next iCount

    ' Fill in the data.
    FillRow(wrdDataDoc, 2, "Steve", "DeBroux",
          "4567 Main Street", "Buffalo, NY  98052")
    FillRow(wrdDataDoc, 3, "Jan", "Miksovsky",
          "1234 5th Street", "Charlotte, NC  98765")
    FillRow(wrdDataDoc, 4, "Brian", "Valentine",
          "12348 78th Street  Apt. 214", "Lubbock, TX  25874")

    ' Save and close the file.
    wrdDataDoc.Save()
    wrdDataDoc.Close(False)
End Sub

0 个答案:

没有答案