We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace a merge Field with a table

Hi All ,

I would like to replace a merge field with a table created using Aspose Words.

Following are the steps which i would like to take place.

I will have a merge field called “table” .

I want to remove that merge field from the document and at the place of that merge field i wanted to replace a table ,which i created using Aspose Words.

Regards,
Jaswanth

Hi Jaswanth,


Thanks for your inquiry. You can simply move the cursor to target merge field and then insert Table. Please see the following articles:

http://www.aspose.com/docs/display/wordsnet/Moving+the+Cursor
http://www.aspose.com/docs/display/wordsnet/Inserting+a+Table+using+DocumentBuilder

I hope, this helps.

Best regards,

Hi ,

Thank You for the prompt Reply .
The problem now is ,I have a merge field ,which is repeated multiple times in the word document .

Like 3 to 4 times ,Now i need to insert the same table at all the places where there is a merge field .

I tried using MovetoMergeField , its just inserting at the first merge field location only ,ignoring the rest of the same merge fields .

Is there any other way ,i can the count of a specific merge field repeated in the document .

Thanks,
Jaswanth

Hi Jaswanth,


Thanks
for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.7.0) from here and let us know how it goes on your side. Please use the following code example to achieve your requirements. I have attached the input and output documents with this post for your kind reference.

Note that DocumentBuilder.MoveToMergeField method (String) moves the cursor to a position just beyond the specified merge field and removes the merge field.

<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:DoNotShowPropertyChanges/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>AR-SA</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>

Document doc = new Document(MyDir + "Test28.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToMergeField("Test1");

// We call this method to start building the table.

builder.StartTable();

builder.InsertCell();

builder.Write("Row 1, Cell 1 Content.");

// Build the second cell

builder.InsertCell();

builder.Write("Row 1, Cell 2 Content.");

// Call the following method to end the row and start a new row.

builder.EndRow();

// Build the first cell of the second row.

builder.InsertCell();

builder.Write("Row 2, Cell 1 Content");

// Build the second cell.

builder.InsertCell();

builder.Write("Row 2, Cell 2 Content.");

builder.EndRow();

// Signal that we have finished building the table.

builder.EndTable();

builder.MoveToMergeField("Test1");

// We call this method to start building the table.

builder.StartTable();

builder.InsertCell();

builder.Write("Row 1, Cell 1 Content.");

// Build the second cell

builder.InsertCell();

builder.Write("Row 1, Cell 2 Content.");

// Call the following method to end the row and start a new row.

builder.EndRow();

// Build the first cell of the second row.

builder.InsertCell();

builder.Write("Row 2, Cell 1 Content");

// Build the second cell.

builder.InsertCell();

builder.Write("Row 2, Cell 2 Content.");

builder.EndRow();

// Signal that we have finished building the table.

builder.EndTable();

doc.Save(MyDir + "Out.docx");



If you still face problem, please share your input and expected output document here for testing. I will investigate the issue and provide you more information.


What if i have 100 's Test1 merge field.


I should keep a for loop and keep looping .

Okay I am even fine with that.

But is there any way i can know ,the count of a certian merge field ,how many a time it is replicated in the document ?


So i can write down the for loop ? because i right now i dont know its count . Because document is generated of fly .

Jaswanth


Hi,


Thanks for your inquiry. You can determine how many times a particular merge field occurs in document by using the following code:

string[] fieldNames = doc.MailMerge.GetFieldNames();

var counts = fieldNames

.GroupBy(w => w)

.Select(g => new { fieldName = g.Key, Count = g.Count() })

.ToList();

foreach (var str in counts)

Console.WriteLine(str.fieldName

  • " -> " + str.Count);

I hope, this helps.

Best regards,