Telerik Forums
UI for WinForms Forum
1 answer
97 views

Hello all

(sorry for my english)

I tried to use the example 5 and 6 on this page :

 https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/editing/mail-merge

But it don't work.

"MERGEFIELD" work but "MERGEFIELD TableStart:" don't. 

What did i do wrong ?

 

Thank you very much for answers :)

Please see Below the code :

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.IO
Imports System.Linq
Imports System.Reflection
Imports System.Text
Imports System.Windows
Imports System.Windows.Forms
Imports Telerik.Windows.Documents.Common.FormatProviders
Imports Telerik.Windows.Documents.Flow.FormatProviders.Docx
Imports Telerik.Windows.Documents.Flow.FormatProviders.Html
Imports Telerik.Windows.Documents.Flow.FormatProviders.Rtf
Imports Telerik.Windows.Documents.Flow.FormatProviders.Txt
Imports Telerik.Windows.Documents.Flow.Model
Imports Telerik.Windows.Documents.Flow.Model.Editing
Imports Telerik.Windows.Documents.Flow.Model.Styles
Imports Telerik.Windows.Documents.Spreadsheet.Model
Imports Telerik.Windows.Documents.Core
Imports Telerik.Windows.Documents.Flow
Imports Telerik.WinForms.Documents.FormatProviders.Pdf

Module M_TestGenerationDoc

    Private ReadOnly providers As Dictionary(Of IFormatProvider(Of RadFlowDocument), String)
    Private chemin As String = "C:\Temp\MODELE\"
    Private selectedFormat As String = "Docx"

Public Sub S_TestFusion()
        Dim document As RadFlowDocument = New RadFlowDocument()
        Dim editor As RadFlowDocumentEditor = New RadFlowDocumentEditor(document)

        editor.InsertParagraph()
        editor.InsertField("MERGEFIELD TeamName", "")
        editor.InsertParagraph()
        editor.InsertText("Players:")

        Dim playersTable As Table = editor.InsertTable(2, 2)
        playersTable.PreferredWidth = New TableWidthUnit(TableWidthUnitType.Percent, 100)
        document.StyleRepository.AddBuiltInStyle(BuiltInStyleNames.TableGridStyleId)
        playersTable.StyleId = BuiltInStyleNames.TableGridStyleId

        playersTable.Rows(0).Cells(0).Blocks.AddParagraph().Inlines.AddRun("First Name")
        playersTable.Rows(0).Cells(1).Blocks.AddParagraph().Inlines.AddRun("Last Name")

        Dim firstNameParagraph As Paragraph = playersTable.Rows(1).Cells(0).Blocks.AddParagraph()
        editor.MoveToParagraphStart(firstNameParagraph)
        editor.InsertField("MERGEFIELD TableStart:Players", "")
        editor.InsertField("MERGEFIELD FirsName", "")

        Dim lastNameParagraph As Paragraph = playersTable.Rows(1).Cells(1).Blocks.AddParagraph()
        editor.MoveToParagraphStart(lastNameParagraph)
        editor.InsertField("MERGEFIELD LastName", "")
        editor.InsertField("MERGEFIELD TableEnd:Players", "")

        Dim mailMergeResult As RadFlowDocument = document.MailMerge(GetTeams())


        SaveDocument(mailMergeResult, selectedFormat)

        MsgBox("Fichier sauvegardé")

    End Sub

 Public Function SaveDocument(ByVal document As RadFlowDocument, ByVal selectedFormat As String) As Boolean
        Dim formatProvider As IFormatProvider(Of RadFlowDocument) = Nothing

        Try
            Select Case selectedFormat
                Case "Docx"
                    formatProvider = New DocxFormatProvider()
                Case "Rtf"
                    formatProvider = New RtfFormatProvider()
                Case "Html"
                    formatProvider = New HtmlFormatProvider()
                Case "Txt"
                    formatProvider = New TxtFormatProvider()
                Case "Pdf"
                    formatProvider = New PdfFormatProvider()
            End Select

            If formatProvider Is Nothing Then
                Return False
            End If

            Dim dialog As SaveFileDialog = New SaveFileDialog()
            dialog.FileName = "Result"
            dialog.Filter = String.Format("{0} files|*{1}|All files (*.*)|*.*", selectedFormat, formatProvider.SupportedExtensions.First())
            dialog.FilterIndex = 1

            Using output As Stream = File.OpenWrite(chemin + "test_fusion.docx")
                formatProvider.Export(document, output)
            End Using

            Return True

        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        End Try


    End Function


Public Function GetTeams() As List(Of Team)
        Dim teams = New List(Of Team)()
        Dim team1 = New Team()
        team1.TeamName = "Team 1"
        team1.Players.Add(New Player() With {
            .FirsName = "John",
            .LastName = "Baker"
        })
        team1.Players.Add(New Player() With {
            .FirsName = "Sam ",
            .LastName = "Wayne"
        })
        teams.Add(team1)
        Dim team2 = New Team()
        team2.TeamName = "Team 2"
        team2.Players.Add(New Player() With {
            .FirsName = "Patrick",
            .LastName = "Gibbs"
        })
        team2.Players.Add(New Player() With {
            .FirsName = "Oscar",
            .LastName = "Stevens"
        })
        teams.Add(team2)
        Return teams
    End Function

    Public Class Team
        Public Property TeamName As String
        Public Property Players As List(Of Player)

        Public Sub New()
            Me.Players = New List(Of Player)()
        End Sub

    End Class

    Public Class Player
        Public Property FirsName As String
        Public Property LastName As String
    End Class

End Module


Vladislav
Telerik team
 answered on 17 Feb 2022
Narrow your results
Selected tags
Tags
GridView
Scheduler and Reminder
Treeview
Dock
RibbonBar
Themes and Visual Style Builder
ChartView
Calendar, DateTimePicker, TimePicker and Clock
Buttons, RadioButton, CheckBox, etc
DropDownList
ComboBox and ListBox (obsolete as of Q2 2010)
ListView
Chart (obsolete as of Q1 2013)
Form
PageView
MultiColumn ComboBox
TextBox
RichTextEditor
Menu
PropertyGrid
RichTextBox (obsolete as of Q3 2014 SP1)
Panelbar (obsolete as of Q2 2010)
PivotGrid and PivotFieldList
Tabstrip (obsolete as of Q2 2010)
MaskedEditBox
CommandBar
PdfViewer and PdfViewerNavigator
ListControl
Carousel
Diagram, DiagramRibbonBar, DiagramToolBox
Panorama
GanttView
New Product Suggestions
Toolstrip (obsolete as of Q3 2010)
AutoCompleteBox
Label
VirtualGrid
ContextMenu
Spreadsheet
Panel
Visual Studio Extensions
TitleBar
SplitContainer
Map
DesktopAlert
ProgressBar
Rotator
TrackBar
MessageBox
CheckedDropDownList
SpinEditor
StatusStrip
CheckedListBox
Wizard
ShapedForm
SyntaxEditor
TextBoxControl
LayoutControl
DateTimePicker
CollapsiblePanel
Conversational UI, Chat
CAB Enabling Kit
TabbedForm
DataEntry
GroupBox
ScrollablePanel
WaitingBar
ImageEditor
ScrollBar
Tools - VSB, Control Spy, Shape Editor
BrowseEditor
DataFilter
ColorDialog
FileDialogs
Gauges (RadialGauge, LinearGauge, BulletGraph)
ApplicationMenu
RangeSelector
CardView
WebCam
BindingNavigator
PopupEditor
RibbonForm
Styling
TaskBoard
Barcode
ColorBox
Callout
PictureBox
VirtualKeyboard
FilterView
Accessibility
DataLayout
NavigationView
ToastNotificationManager
ValidationProvider
CalculatorDropDown
Localization
TimePicker
FontDropDownList
Licensing
BreadCrumb
ButtonTextBox
LocalizationProvider
Dictionary
Overlay
Security
Separator
SparkLine
TreeMap
StepProgressBar
SplashScreen
ToolbarForm
NotifyIcon
Rating
TimeSpanPicker
BarcodeView
Calculator
OfficeNavigationBar
Flyout
TaskbarButton
HeatMap
SlideView
PipsPager
AIPrompt
+? more
Top users last month
Mark
Top achievements
Rank 1
Yurii
Top achievements
Rank 1
Leland
Top achievements
Rank 2
Iron
Iron
Iron
Hon
Top achievements
Rank 1
Iron
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Mark
Top achievements
Rank 1
Yurii
Top achievements
Rank 1
Leland
Top achievements
Rank 2
Iron
Iron
Iron
Hon
Top achievements
Rank 1
Iron
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?