I have created word doc with merge fields. Merge fields in word doc would look like this:
{ MERGEFIELD TestValue }.
What I need to do is to remove the white space at the beginning and end of the field. So it should look like this:
{MERGEFIELD TestValue}.
I'm using Microsoft.Office.Interop.Word to generate the document.
Here is sample of the code:
Word.Field fldMerge;
bool bFound;
oRng.Find.ClearFormatting();
oRng.Find.Forward = true;
oRng.Find.Wrap = Word.WdFindWrap.wdFindStop;
oRng.Find.Format = false;
oRng.Find.MatchCase = false;
oRng.Find.MatchWholeWord = false;
oRng.Find.MatchWildcards = false;
oRng.Find.MatchSoundsLike = false;
oRng.Find.MatchAllWordForms = false;
oRng.Find.Text = sOldTag.Trim();
bFound = oRng.Find.Execute();
while (bFound)
{
fldMerge = oRng.Fields.Add(oRng, Word.WdFieldType.wdFieldMergeField, sNewTag.Trim(), false);
oRng = fldMerge.Result;
oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
oRng.MoveStart(Word.WdUnits.wdCharacter, 2);
oRng.End = oRng.Document.Content.End;
oRng.Find.Text = sOldTag.Trim();
bFound = oRng.Find.Execute();
}
This is sample of the code of how to create the merge field