NEWS

[DEVEXPRESS] Hướng dẫn sử dụng Mail Merge trong Rich Edit Control VB.NET

[DEVEXPRESS] Hướng dẫn sử dụng Mail Merge trong Rich Edit Control VB.NET
Đăng bởi: Thảo Meo - Lượt xem: 14376 07:47:44, 16/05/2017DEVEXPRESS   In bài viết

Bài viết hôm nay, mình xin hướng dẫn các bạn cách sử dụng Mail Merge trong Rich Edit Control của Devexpress bằng VB.NET.

Vậy Mail Merge là gì? Hướng dẫn sử dụng Mail Merge trong  Devexpress  VB.NET

Trộn thư (Mail Merge) là tính năng hữu ích trong Microsoft Word, tính năng này giúp các bạn giảm thiểu thời gian khi xử lý các dạng chèn danh sách vào một mẫu cố định như: thư mời, thông báo, thư cảm ơn, phiếu điểm…

Mặc dù tính năng này rất hữu ích nhưng có nhiều bạn chưa biết hoặc chưa để ý đến tính năng trộn thư này. Các bạn cùng tham khảo bài viết dưới đây để biết cách trộn thư trong Word 2017.

* Có nhiều bạn hỏi tại sao sử dụng Mail Merge, em thấy nó cũng giống tạo Report.

Với Mail merge thì người sử dụng cuối chỉ cần sửa thông tin file word, ví dụ: hợp đồng hay bảng lương thì có thể chạy. Còn nếu các bạn sử dụng Report thì cần phải can thiệp vào code mới chỉnh sửa được.

Giao diện Demo của ứng dụng:

sử dụng mail merge

Source code Mail Merge VB.NET ứng dụng:

Imports System.ComponentModel
Imports System.Text
Imports System.Data.SqlClient
Imports System.Collections
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraRichEdit
Imports DevExpress.Services
Imports MailMerge.ProgressIndicator

Partial Public Class Form1
    Shared Sub New()
        DevExpress.UserSkins.BonusSkins.Register()
        DevExpress.Skins.SkinManager.EnableFormSkins()
    End Sub
    Public Sub New()
        InitializeComponent()
    End Sub
    Dim con As New SqlConnection
    Public Sub Taoketnoi()
        Dim str As String = "server=192.168.0.3;Initial Catalog=HOB2016;User ID=sa; password=minh123"
        con.ConnectionString = str
        con.Open()
    End Sub
    Public Sub Dongketnoi()
        con.Close()
    End Sub
    Public Function LayDulieu(sql As String) As DataTable
        Taoketnoi()
        Dim dt As New DataTable
        Dim da As New SqlDataAdapter
        da.SelectCommand = New SqlCommand(sql, con)
        da.Fill(dt)
        Dongketnoi()
        Return dt
    End Function
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dt As DataTable = LayDulieu("select * from view_nhanvien")
        GridControl1.DataSource = dt
        lbl_manv.DataBindings.Clear()
        lbl_manv.DataBindings.Add("Text", dt, "manv")
        inputData.LoadDocument("certificate.doc", DocumentFormat.Doc)

    End Sub

    Private Sub lbl_manv_TextChanged(sender As Object, e As EventArgs) Handles lbl_manv.TextChanged
        Label1.Text = lbl_manv.Text
    End Sub

    Private Sub btnprint_Click(sender As Object, e As EventArgs) Handles btnprint.Click

        Dim dt As DataTable = LayDulieu("select TENNV, diachi1 as diachi from view_nhanvien where manv='" & lbl_manv.Text & "'")
        inputData.Options.MailMerge.DataSource = dt
        inputData.Options.MailMerge.ViewMergedData = True
        'XEM MAIL MERGE
        Dim myMergeOptions As MailMergeOptions = inputData.Document.CreateMailMergeOptions()
        'myMergeOptions.FirstRecordIndex = 1
        'myMergeOptions.LastRecordIndex = 10
        myMergeOptions.MergeMode = MergeMode.NewSection
        inputData.Document.MailMerge(myMergeOptions, inputData2.Document)
        inputData2.ShowPrintPreview()

    End Sub

    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click
        Dim dt As DataTable = LayDulieu("select TENNV, diachi1 as diachi from view_nhanvien")
        inputData.Options.MailMerge.DataSource = dt
        inputData.Options.MailMerge.ViewMergedData = True
        'XEM MAIL MERGE
        Dim myMergeOptions As MailMergeOptions = inputData.Document.CreateMailMergeOptions()
        'myMergeOptions.FirstRecordIndex = 1
        'myMergeOptions.LastRecordIndex = 10
        myMergeOptions.MergeMode = MergeMode.NewSection
        inputData.Document.MailMerge(myMergeOptions, inputData2.Document)
        inputData2.ShowPrintPreview()
    End Sub

    Private Sub inputData_MailMergeStarted(sender As Object, e As MailMergeStartedEventArgs) Handles inputData.MailMergeStarted
        inputData.ReplaceService(Of IProgressIndicationService) _
(New MyProgressIndicatorService(inputData, Me.progressBarControl1))
    End Sub

    Private Sub inputData_MailMergeFinished(sender As Object, e As MailMergeFinishedEventArgs) Handles inputData.MailMergeFinished
        inputData.RemoveService(GetType(IProgressIndicationService))
    End Sub

    Private Sub inputData_MailMergeRecordStarted(sender As Object, e As MailMergeRecordStartedEventArgs) Handles inputData.MailMergeRecordStarted
        System.Threading.Thread.Sleep(100)
    End Sub

    Private Sub inputData_MailMergeRecordFinished(sender As Object, e As MailMergeRecordFinishedEventArgs) Handles inputData.MailMergeRecordFinished
        'e.RecordDocument.AppendDocumentContent("Docsungalow.docx", DocumentFormat.OpenXml)
    End Sub

    Private Sub SimpleButton3_Click(sender As Object, e As EventArgs) Handles SimpleButton3.Click
        ' xuất file mail merge
        Dim dt As DataTable = LayDulieu("select TENNV, diachi1 as diachi from view_nhanvien where manv='" & lbl_manv.Text & "'")
        Dim myMergeOptions As MailMergeOptions = inputData.Document.CreateMailMergeOptions()
        myMergeOptions.DataSource = dt
        'myMergeOptions.FirstRecordIndex = 1
        'myMergeOptions.LastRecordIndex = 3
        myMergeOptions.MergeMode = MergeMode.NewSection

        Dim fileDialog As New SaveFileDialog()
        fileDialog.Filter = "MS Word 2007 documents (*.docx)|*.docx|All files (*.*)|*.*"
        fileDialog.FilterIndex = 1
        fileDialog.RestoreDirectory = True


        Dim dialogResult_Renamed As DialogResult = fileDialog.ShowDialog()
        If dialogResult_Renamed = System.Windows.Forms.DialogResult.OK Then
            Dim fName As String = fileDialog.FileName
            If fName <> "" Then
                inputData.Document.MailMerge(myMergeOptions, fileDialog.FileName, DocumentFormat.OpenXml)
                System.Diagnostics.Process.Start(fileDialog.FileName)
            End If
        End If
    End Sub
End Class

- Tiếp là tạo thêm một class: MyProgressIndicator.vb, dùng để hiển thị phần trăm progress bar, nếu bạn chạy Mail merge nhiều dữ liệu.

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports DevExpress.XtraEditors
Imports DevExpress.Services
Imports System.Windows.Forms
Imports System.Drawing

Namespace ProgressIndicator
#Region "#myprogressindicator"
    Friend Class MyProgressIndicatorService
        Implements IProgressIndicationService
        Private _Indicator As ProgressBarControl
        Public Property Indicator() As ProgressBarControl
            Get
                Return _Indicator
            End Get
            Set(ByVal value As ProgressBarControl)
                _Indicator = value
            End Set
        End Property

        Public Sub New(ByVal provider As IServiceProvider, ByVal indicator As ProgressBarControl)
            _Indicator = indicator
        End Sub

#Region "IProgressIndicationService Members"

        Private Sub Begin(ByVal displayName As String, ByVal minProgress As Integer, ByVal maxProgress As Integer, ByVal currentProgress As Integer) Implements IProgressIndicationService.Begin
            _Indicator.Properties.Minimum = minProgress
            _Indicator.Properties.Maximum = maxProgress
            _Indicator.Properties.ShowTitle = True
            _Indicator.EditValue = currentProgress
            _Indicator.Refresh()
            _Indicator.Show()
        End Sub

        Private Sub [End]() Implements IProgressIndicationService.End
            _Indicator.Refresh()
            _Indicator.Hide()
        End Sub

        Private Sub SetProgress(ByVal currentProgress As Integer) Implements IProgressIndicationService.SetProgress
            _Indicator.EditValue = currentProgress
            _Indicator.Refresh()
        End Sub
#End Region
    End Class
#End Region ' #myprogressindicatorsindicator
End Namespace

VIDEO DEMO ỨNG DỤNG:

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn sử dụng Mail Merge trong Rich Edit Control VB.NET
Đăng bởi: Thảo Meo - Lượt xem: 14376 07:47:44, 16/05/2017DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.