Hello all
(sorry for my english)
I tried to use the example 5 and 6 on this page :
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