Create, Write and Save Word in C#, VB.NET

A new created Word document can be written with contents and saved to specified path. This guide demonstrates solution to create, write and save Word in C# and VB.NET.

The basic operations of Word are to generate, write contents and save. First of all, users can create a blank Word document and then write what they want to say in this document and finally save to specified path. This guide focuses on presenting how to create, write and save Word in C# and VB.NET via Spire.Doc for .NET. The screenshot below is result after programming with following steps.

Operate Word

Create Word

Spire.Doc for .NET offers a Document class to enable developers to initialize a new Document instance. This instance is a new blank Word document.

[C#]
            //Create New Word
            Document doc = new Document();
[VB.NET]
	     'Create New Word
            Dim doc As New Document()

Write Contents in Word

Generally speaking, contents are written in a paragraph which is contained in section of document. Therefore, you need to add a new section firstly by invoking method Document.AddSection(). And add a new paragraph in new section by invoking Section.AddParagraph(). After that, you can write contents in paragraph by invoking Paragraph.AppendText(string text) method.

[C#]
            //Add Section
            Section section = doc.AddSection();
            //Add Paragraph
            Paragraph Para = section.AddParagraph();
            //Append Text
            Para.AppendText("Spire.Doc for .NET, a professional .NET Word component, "
            +"enables developers to perform a large range of tasks on Word document(from Version Word97-2003 to Word 2010) "
            +"for .NET in C# and VB.NET."
            +"This libray is specially designed for .NET developers to help them"
            +"to create any WinForm and ASP.NET Web applications to create, open, write, edit, save and convert"      
            +"Word document without Microsoft Office and any other third-party tools installed on system.");
[VB.NET]
            'Add Section
            Dim section As Section = doc.AddSection()
            'Add Paragraph
            Dim Para As Paragraph = section.AddParagraph()
            'Append Text
            Para.AppendText("Spire.Doc for .NET, a professional .NET Word component, " & "enables developers to perform a large range of tasks on Word document(from Version Word97-2003 to Word 2010) " & "for .NET in C# and Visual Basic." & "This libray is specially designed for .NET developers to help them" & "to create any WinForm and ASP.NET Web applications to create, open, write, edit, save and convert" & "Word document without Microsoft Office and any other third-party tools installed on system.")

Save Word

Invoke SaveToFile method of Document class to save the written Word. The parameter passed to this method is string fileName. If you want to set extension as .doc or .docx, you need to pass the other parameter FileFormat fileFormat to this method.

[C#]
            //Save Word
            doc.SaveToFile("OperateWord.docx", FileFormat.Docx);
[VB.NET]
            'Save Word
            doc.SaveToFile("OperateWord.docx", FileFormat.Docx)

Spire.Doc, professional Word component, is specially designed for developers to fast generate, write, modify and save Word documents in .NET, Silverlight and WPF with C# and VB.NET. Also, it supports conversion between Word and other popular formats, such as PDF, HTML, Image, Text and so on, in .NET and WPF platform.