Word Replace in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to replace one word to another in a Word document.

private void button1_Click(object sender, EventArgs e)
{
    //Create word document
    Document document = new Document();
    document.LoadFromFile(@"..\..\..\..\..\..\Data\FindAndReplace.doc");

    //Replace text
    document.Replace(this.textBox1.Text, this.textBox2.Text,true,true);

    //Save doc file.
    document.SaveToFile("Sample.doc", FileFormat.Doc);

    //Launching the MS Word file.
    WordDocViewer("Sample.doc");
}

private void WordDocViewer(string fileName)
{
    try
    {
        System.Diagnostics.Process.Start(fileName);
    }
    catch { }
}

 Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
	'Create word document
	Dim document_Renamed As New Document()
	document_Renamed.LoadFromFile("..\..\..\..\..\..\Data\FindAndReplace.doc")

	'Replace text
	document_Renamed.Replace(Me.textBox1.Text, Me.textBox2.Text,True,True)

	'Save doc file.
	document_Renamed.SaveToFile("Sample.doc", FileFormat.Doc)

	'Launching the MS Word file.
	WordDocViewer("Sample.doc")
End Sub

Private Sub WordDocViewer(ByVal fileName As String)
	Try
		Process.Start(fileName)
	Catch
	End Try
End Sub

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details