Positioning tables when replacing text

Feb 20, 2015 at 2:24 PM
I'm working with a template .docx document filled with a lot of tags.
I loop through these tags and replace them with either simple text (which works fine) or tables.

I'm having problems positioning the tables correctly.

If I use the following code the tables seems to initially be placed at the correct position but when I do further replaces they end up misplaced.
var finds = document.FindAll(tag);
foreach (var find in finds)
    document.InsertTable(find, table);
document.ReplaceText(tag, "");
I can't use Paragraph.InsertTableAfterSelf or Paragraph.InsertTableBeforeSelf since the tag might be placed in the middle of a paragraph.

How do I replace the tags with tables and keep them in fixed relative position when doing further replaces?