I am using the NetOffice library to edit a Word document. In my document, I have a field "{NAME}" that I would like to replace with a value "John Smith". However, executing the following code does not work. Find.Execute returns false, indicating failure, and no change is reflected in document.Content.Text.
// Open the template
Application word = new Application();
Document document = word.Documents.Open(fileName, false, true);
// Set up initial behavior in word
word.Visible = false;
// Replace template with final values
foreach (LetterField field in fields)
{
document.Content.Find.ClearFormatting();
document.Content.Find.Text = "{" + field.Key + "}";
document.Content.Find.Replacement.ClearFormatting();
document.Content.Find.Replacement.Text = field.Value;
document.Content.Find.Execute(null, null, null, null, null, null, null, null, null, null, WdReplace.wdReplaceAll);
}
I also tried manually replacing document.Content.Text, but this removes all formatting from the page, which is also undesirable. How can I replace text in a document in NetOffice?
I notice that setting document.Content.Find.Text does not seem to do anything, as checking the value still yields "", even after setting it to something else. Is this intended behavior, or am I missing something?
The document contains the following (copied and pasted):
Date: {DATE}
{NAME}
{ADDRESSLINE}
{ADDRESSCITY}, {ADDRESSSTATE} {ADDRESSCODE}
Some fields are as follows:
<"NAME", "John Smith">
<"DATE", "10/28/2021">
