Free Support Forum - aspose.com

FONT Issue During MailMerge Fields in a table

Hi,

I met a strange issue in a table MailMerge, I post a solution attached to this thread.
VB.NET VS2005.

Try it and see the result in the document in page 13.

The issue is in the model "TestImpartial\TestImpartial\bin\Debug\data\Modèles\ModeleAmV2.doc"
Chapter "Evaluation de l’état de conservation amiante non
friable"

You can see in the table 2 detail lines which must be in red. Only few fields are in italic, bold, red style not the others.

I copy this chapter in a new document and runs this document, the table is well fed. Is there something wrong in the first document

Please, could you check what’s wrong with that ?

Thank’s very much.

Emmanuel

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I can’t reproduce this issue on my side. I tried to use the following code:

DataTable table = new DataTable("TableATGrEvaNonFri");

table.Columns.Add("NoRep");

table.Columns.Add("Loc");

table.Columns.Add("Mat");

table.Columns.Add("CVD");

table.Columns.Add("ESD");

table.Columns.Add("Photo");

table.Columns.Add("Pre");

table.Columns.Add("Pres");

for (int i = 0; i < 10; i++)

{

table.Rows.Add(new object[] { "NoRep", "Loc", "Mat", "CVD", "ESD", "Photo", "Pre", "Pres" });

}

Document doc = new Document(@"Test181\in.doc");

doc.MailMerge.ExecuteWithRegions(table);

doc.Save(@"Test181\out.doc");

Could you please simplify your code?

Best regards.

Ok, I simplified the code, I think you can try it.
I attached the image of out.doc
In the last method “EcritTexte” I set the variable Couleur = “red” , the fields are written in red.

Dim table As New DataTable(“TableATGrEvaNonFri”)
table.Columns.Add(“NoRep”)
table.Columns.Add(“Loc”)
table.Columns.Add(“Mat”)
table.Columns.Add(“CVD”)
table.Columns.Add(“ESD”)
table.Columns.Add(“Photo”)
table.Columns.Add(“Pre”)
table.Columns.Add(“Pres”)
Dim noRep As Integer = 0
For i As Integer = 0 To 9
noRep = i
table.Rows.Add(New Object() {noRep, “Loc” & noRep, “Mat” & noRep, “CVD” & noRep, “ESD” & noRep, “Photo” & noRep, _
“Pre” & noRep, “Pres” & noRep})
Next
Dim doc As New Document(chem & “\ModeleAmV2.doc”)
mBuilder = New Aspose.Words.DocumentBuilder(doc)
AddHandler doc.MailMerge.MergeField, AddressOf HandleMergeField

doc.MailMerge.ExecuteWithRegions(table)
doc.Save(“D:\DéveloppementV6\AsposeTestImpartial\TestImpartial\out.doc”)

Sub HandleMergeField(ByVal sender As Object, ByVal e As Aspose.Words.Reporting.MergeFieldEventArgs)

If e.FieldValue Is Nothing Then Exit Sub
If e.FieldValue Is Convert.DBNull Then Exit Sub

Dim Couleur As String = “” ’ La couleur à mettre sur le champ

If TypeOf (e.FieldValue) Is Boolean Then
’ Ecrire une case à cocher
mBuilder.MoveToMergeField(e.FieldName)
Dim checkBoxName As String = String.Format("{0}{1}", e.FieldName, e.RecordIndex)
mBuilder.InsertCheckBox(checkBoxName, CBool(e.FieldValue), 0)
Exit Sub
Else
’ Ecrire un texte de champ
EcritTexte(e, Couleur)
End If

End Sub

Sub EcritTexte(ByVal e As Aspose.Words.Reporting.MergeFieldEventArgs, Optional ByVal Couleur As String = “”)

Dim pgs() As String = Split(e.FieldValue, Chr(11)) ’ Marques de paragraphes
Dim a As String = e.FieldValue
If pgs.Length > 1 Then
’ Plusieurs lignes
mBuilder.MoveToMergeField(e.FieldName)
Dim ixPg As Integer
For ixPg = 0 To pgs.Length - 1
EcritTexte(pgs(ixPg), CBool(ixPg <> pgs.Length - 1), Couleur)
Next
Else
’ Une seule ligne
If Couleur <> “” Then
mBuilder.MoveToMergeField(e.FieldName)
EcritTexte(e.FieldValue, False, Couleur)
Else
mBuilder.MoveToMergeField(e.FieldName)
EcritTexte(e.FieldValue, False, Couleur)
End If
End If

End Sub

Sub EcritTexte(ByVal Chaine As String, ByVal AvecMarque As Boolean, ByVal Couleur As String)
’ Il faut avoir préalablement fait un MoveToMergeField
Dim st() As String = Split(Chaine, Chr(0))
If st.Length > 1 Then
Chaine = st(0)
Couleur = st(1)
End If
Couleur = “red”
If Chaine <> “” Then
mBuilder.PushFont()
If Couleur <> “” Then
Select Case Couleur
Case “red”
mBuilder.Font.Color = System.Drawing.Color.Red
mBuilder.Font.Bold = True
mBuilder.Font.Italic = True
Case “green”
mBuilder.Font.Color = System.Drawing.Color.Green
mBuilder.Font.Bold = True
Case “blue”
mBuilder.Font.Color = System.Drawing.Color.Blue
Case “magenta”
mBuilder.Font.Color = System.Drawing.Color.Magenta
mBuilder.Font.Italic = True
Case Else
mBuilder.Font.Color = System.Drawing.Color.Black
End Select
End If
mBuilder.Write(Chaine)
mBuilder.PopFont()
If AvecMarque Then
mBuilder.Writeln("")
End If
End If
End Sub


Regards,

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for additional information. This occurs because your document contains several fields having the same name. You can easy solve this problem using the following code:

mBuilder.MoveToField(e.Field, True)

e.Field.Remove()

instead

mBuilder.MoveToMergeField(e.FieldName)

Best regards.

Thank you very much Alexey for your help, It’s solved. I can go ahead.

It’s really a good tool.

Best regards,

Emmanuel