Edit Word Document in C#, VB.NET

In order to correct wrong spellings or add some new contents in a Word document, users need to edit an existing Word document. This guide demonstrates a solution to edit Word document in C# and VB.NET.

Spire.Doc for .NET, wonderful .NET Word component, offers a Paragraph class, which enables users to edit contents in paragraphs through set its properties. In this example, the title is updated and new text is added in paragraph two (Title is paragraph one). The editing result is shown as following screenshot.

Edit Word Document

Firstly, declare a Paragraph instance and its value is set as paragraph one (title). Set its Text property to update the original contents. Secondly, declare another Paragraph instance and its value is set as Paragraph two. Invoke Paragraph.AppendText method to add new contents for this paragraph. The overload passed to this method is string text. For distinguishing new contents and existing contents, new contents are formatted in this example. Declare a TextRange instance and set its value as new added contents. Set CharacterFormat properties for this TextRange, including FontName, FontSize and TextColor. Download and install Spire.Doc for .NET and follow the code below to edit Word document.

using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace EidtWord
    class Program
        static void Main(string[] args)
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx");

            //Update Text of Title
            Section section = document.Sections[0];
            Paragraph para1 = section.Paragraphs[0];
            para1.Text = "Spire.Doc for .NET Introduction";

            //Add New Text
            Paragraph para2 = section.Paragraphs[1];
            TextRange tr=para2.AppendText("Spire.Doc for .NET is stand-alone"
            +"to enables developers to operate Word witout Microsoft Word installed.");
            tr.CharacterFormat.FontName = "Cataneo BT";
            tr.CharacterFormat.TextColor = Color.YellowGreen;
            //Save and Launch
            document.SaveToFile("Edit Word.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Edit Word.docx");
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace EidtWord
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx")

            'Update Text of Title
            Dim section As Section = document.Sections(0)
            Dim para1 As Paragraph = section.Paragraphs(0)
            para1.Text = "Spire.Doc for .NET Introduction"

            'Add New Text
            Dim para2 As Paragraph = section.Paragraphs(1)
            Dim tr As TextRange = para2.AppendText("Spire.Doc for .NET is stand-alone" &
                                                   "to enables developers to operate Word witout Microsoft Word installed.")
            tr.CharacterFormat.FontName = "Cataneo BT"
            tr.CharacterFormat.FontSize = 12
            tr.CharacterFormat.TextColor = Color.YellowGreen

            'Save and Launch
            document.SaveToFile("Edit Word.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Edit Word.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, an easy-to-use component to operate Word document, allows developers to fast generate, write, edit and save Word (Word 97-2003, Word 2007, Word 2010) in C# and VB.NET for .NET, Silverlight and WPF.