General discussion
December 20, 2010 at 08:14 PM
tomluhong

Edit word document

by tomluhong . Updated 15 years, 8 months ago

If you want to edit an existing word document, you should open it first. So, you need to know How to Open a Word Document at first.
To edit a paragraph, you need to reference a Spire.Doc.Documents.Paragraph object which represents the physical paragraph which you want to edit. In this demo, I will edit the second pragraph of the first section of my word document, we suppose I have opened it and reference it via variable document.
[C#]
Section section = document.Sections[0];
Paragraph paragraph = section.Paragraphs[1];
[Visual Basic]
Dim section As Section = document.Sections(0)
Dim paragraph As Paragraph = section.Paragraphs(1)
Now, we can edit it as we want.
[C#]
//update the original text
paragraph.Text = “Updated”;

//append a new text
paragraph.AppendText(” by Spire.Doc.”);
[Visual Basic]
‘update the original text
paragraph.Text = “Updated”

‘append a new text
paragraph.AppendText(” by Spire.Doc.”)
Full example
[C#]
//open the word document
Document document = new Document(“Editing.doc”);

//get the paragraph which we want to edit
Section section = document.Sections[0];
Paragraph paragraph = section.Paragraphs[1];

//update the original text
paragraph.Text = “Updated”;

//append a new text
paragraph.AppendText(” by Spire.Doc.”);

//save to a file
document.SaveToFile(“Sample.doc”);
[Visual Basic]
‘open the word document
Dim document As New Document(“Editing.doc”)

‘get the paragraph which we want to edit
Dim section As Section = document.Sections(0)
Dim paragraph As Paragraph = section.Paragraphs(1)

‘update the original text
paragraph.Text = “Updated”

‘append a new text
paragraph.AppendText(” by Spire.Doc.”)

‘save to a file
document.SaveToFile(“Sample.doc”)

This discussion is locked

All Comments