Question

I'm building a Word 2013 addin that will fill in information in templates. So the documents opens, a form appears, users fill in data, that data appears in the form. That's the easy part. If users make changes in the document and then click a button, the form should reappear and the modified values should be filled in into the forms textboxes, combo boxes, etc.

The form part is easy, I've got that down. The hard part is puting text in the word document and getting it back out again.

Bookmarks seem to dissapear if I do Bookmark.Range.Text = txtExample.Text;. And Content Controls are not availalbe in an addin, they are read only apparently.

Was it helpful?

Solution

I'm officially an idiot, it was soo simple. See code below, hope it helps future idiots improve their coding skills.

private void InsertIntoBookmark(string bookmarkName, string text)
{
    if (Document != null && Document.Bookmarks.Exists(bookmarkName))
    {
        var range = Document.Bookmarks[bookmarkName].Range;
        Document.Bookmarks[bookmarkName].Delete();
        range.Text = text;

        // replace bookmark
        Document.Bookmarks.Add(bookmarkName, range);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top