Mailo merge and also dyamically add fields


#1

Hi

The application I’m currently working uses Aspose word, I haven’t used it before but need to make changes to some document produced by it. The initial programmer has used templates and the mail merge functionality to output the documents. The guy has wrote some crazy code whereby 2 string are produced and merged into fields with 2 columns in a table in the word document (one string goes into he left column and one into the right). Data in each column is meant to line up. In the example below Mr Smith and Mr Black is one string and Baker and Cook is the other string. It obviously should be a table with multiple rows, not just 2 columns and crazy code to make the text line up....

Column 1 Column 2
Mr Smith Baker
Mr Black Cook

Anyway to cut to the chase can you perform a mail merge using templates and also dynamically add items to the same word document (say just after you did the merge) using your DocumentBuilder class (or some something class). Or if you want to dynamically add items to a word document do you have to use DocumentBuilder exclusively.

Thanks for your help


#2

answer my own question....

just rip the code from the samples, then you could create 2 methods like this and off you go...

public void Run()

{

Document document = new Document(@"C:\Development\AsposeWordLearning\Learning\GenericCoverPage.doc");

//Example of a very simple mail merge to populate fields only once from an array of values.

document.MailMerge.Execute(

new string[] {"ActionNo", "ActionYear", "DocumentTitle", "WCCN"},

new object[] {"James Bond", "Secret Agent", "MI5 Headquarters", "Milbank, London"});

DocumentBuilder builder = new DocumentBuilder(document);

BuildChessBoard(builder);

document.Save(@"C:\Development\AsposeWordLearning\Learning\GenericCoverPage.Output.doc", SaveFormat.FormatDocument);

}

///

/// Creates a chess like table to test borders and shading.

///

public void BuildChessBoard(DocumentBuilder builder)

{

//Move the insertion cursor to a merge field.

builder.MoveToMergeField("MyInsertionPlace1");

//Reset to default font and paragraph formatting.

builder.Font.ClearFormatting();

builder.ParagraphFormat.ClearFormatting();

builder.InsertParagraph();

builder.Writeln("This shows how to build a table in a document.");

builder.Font.Size = 6;

builder.Font.Name = "Tahoma";

builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

//It is possible to change row format before (like here) and also during creation of the row.

builder.RowFormat.Height = 0.4 * 72; //0.4"

builder.RowFormat.HeightRule = HeightRule.Exactly;

//It is possible to change cell format before (like here) and also during creation of the cell.

builder.CellFormat.Width = 0.4 * 72; //0.4"

builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

Borders borders = builder.CellFormat.Borders;

borders.LineStyle = LineStyle.Single;

borders.LineWidth = 2;

borders.Color = System.Drawing.Color.Blue;

for (int y = 0; y < 8; y++)

{

for (int x = 0; x < 8; x++)

{

builder.InsertCell();

builder.Write(string.Format("X{0}, Y{1}", x, y));

//This alternatives cell backgrounds.

bool isBlack = (((x + y) & 0x01) != 0);

builder.CellFormat.Shading.BackgroundPatternColor =

(isBlack) ? System.Drawing.Color.LightGreen : System.Drawing.Color.LightYellow;

}

builder.EndRow();

}

builder.EndTable();

}


#3

Good job, Keith. Glad you've solved it on your own. If you have any more questions, don't hesitate to post them.

Cheers,