MergeField TableStart doesn't work - have you any idea why ?

1 Answer 126 Views
Documentation General Discussions
ADW
Top achievements
Rank 1
ADW asked on 17 Feb 2022, 08:32 AM

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


1 Answer, 1 is accepted

Sort by
1
Vladislav
Telerik team
answered on 17 Feb 2022, 11:21 AM

Hello Julien,

I have tested your code locally and everything works as it should. The examples, referred by you, demonstrate one of our latest features - Nested Mail Merge. The support for it was released with our latest official release - 2022 R1. Can you confirm that you use these libraries? If not - please, update your project references.

You can find attached the small console app, that I have used to locally test this functionality.

Looking forward to your reply.

Regards,
Vladislav
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

ADW
Top achievements
Rank 1
commented on 22 Feb 2022, 08:05 AM

Hello Vladislav,

thank you for your answer.

You have right. I had not seen that i was in 2021 released...

Have a nice day !

Tags
Documentation General Discussions
Asked by
ADW
Top achievements
Rank 1
Answers by
Vladislav
Telerik team
Share this question
or