Word Automation in C# - Replace a Mail Merge field with a Table
I am trying to use Word Automation from C# to take a WORD template
document and create a travel itinerary. This involves adding a number of
table to the template document and writing out as a new document.
Originally I wrote this using bookmarks to define where the additional
tables/text were to be added but I am not keen on this as from a user's
point of view it is quite difficult to identify the insertion points as
the bookmarks do not show up with their 'name' in the template so I
started looking at mail merge fields as these are easily viewed and make
it more obvious where for example a table for Hotel reservations will be
placed.
My problem is that while it is easy enough to replace a Mail Merge Field
with text using something like :-
myMergeField.Select(); oWordApplic.Selection.TypeText("This Text Replaces
the Field in the Template");
I cannot seem to replace the mail merge field with a table. When I was
using bookmarks I used code similar to :-
Microsoft.Office.Interop.Word.Table newTable = oDoc.Tables.Add(range,
rows, columns, ref oMissing, ref oMissing);
newTable.Range.Font.Name = "Verdana";
newTable.Range.Font.Size = 10;
newTable.AllowAutoFit = true;
newTable.AutoFitBehavior(Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitWindow);
where 'range' was the range of the bookmark and this worked perfectly. Is
it possible tod o something similar with mail merge fields or will I have
to revert to using bookmarks?
Thanks for any advice/suggestions.
No comments:
Post a Comment