It is possible that we may make some mistakes when writing something in Word, such as misspelling, catachresis and so on. We can correct mistakes directly, or use “Replace” function.
Replace function in Word is used to replace text or sentence with another one. Because of this function, it is very convenient for users to modify contents to make words or sentences to be used more correctly and appropriately. And it is very useful when we need to replace text which appears many times in document.
In this post, I want to introduce a method to replace text in Word with Silverlight. I prepare a document which is talking about New Zealand. I will change all “New Zealand” in this document as “NZ”.
Note: a component, Spire.Doc for Silverlight is used in this example. So, if you want to use the following code, please DOWNLOAD and install it. And then, add its dll file as reference in your project.
Step 1. Design UserControl
Rename MainPage.xaml as Replace.xaml. Double click it to design UserControl. Firstly, add a label in UserControl and change content. Set format for content, including font style and size. Secondly, add a button to run. Change button background color and content font style. Thirdly, set background color for UserControl.
Step 2. Declare SaveFileDialog
Declare a SaveFileDialog for saving document. Then, set filter for this SaveFileDialog. Filter is used for people to choose format the document is, for example, .doc or .docx. In this example, I set the format as .docx only.
Step 3. Load Document
Right click project to add existed item which is the document which I want to replace text. After adding, click document and change its Build Action as Embedded Resource.
Double click run button to write code. Declare document and assembly. Use foreach sentence to get name string from assembly. If the name string is the same with document name, load this document.
Step 4. Replace Text
Use document.Replace method to replace text. There are four parameters passed to this method, original string the document has, new string to replace original string, bool value to judge if casing sensitive, bool value to judge if replacing whole world.
Step 5. Save Document
Save document through SaveFileDialog which I declare in the first step if it can pop up.
public partial class MainPage : UserControl
private SaveFileDialog saveFiledialog = new SaveFileDialog();
this.saveFiledialog.Filter = “Word Document (*.docx)|*.docx”;
private void button1_Click(object sender, RoutedEventArgs e)
Document document = new Document();
Assembly assembly = this.GetType().Assembly;
foreach (String name in assembly.GetManifestResourceNames())
if (name.EndsWith(“New Zealand.docx”))
using (Stream docStream = assembly.GetManifestResourceStream(name))
document = new Document(docStream, FileFormat.Docx);
document.Replace(“New Zealand”, “NZ”, true, true);
bool? result = this.saveFiledialog.ShowDialog();
if (result.HasValue && result.Value)
using (Stream stream = this.saveFiledialog.OpenFile())