Word Styles in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to Set Word styles.

private void button1_Click(object sender, EventArgs e)
{
    //Create word document
    Document document = new Document();

    //Create a new secition
    Section section = document.AddSection();

    //Create a new paragraph
    Paragraph paragraph = section.AddParagraph();

    //Append Text
    paragraph.AppendText("Builtin Style:");

    foreach (BuiltinStyle builtinStyle in Enum.GetValues(typeof(BuiltinStyle)))
    {
        paragraph = section.AddParagraph();
        //Append Text
        paragraph.AppendText(builtinStyle.ToString());
        //Apply Style
        paragraph.ApplyStyle(builtinStyle);
    }

    //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()

	'Create a new secition
	Dim section_Renamed As Section = document_Renamed.AddSection()

	'Create a new paragraph
	Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()

	'Append Text
	paragraph_Renamed.AppendText("Builtin Style:")

	For Each builtinStyle_Renamed As BuiltinStyle In System.Enum.GetValues(GetType(BuiltinStyle))
		paragraph_Renamed = section_Renamed.AddParagraph()
		'Append Text
		paragraph_Renamed.AppendText(builtinStyle_Renamed.ToString())
		'Apply Style
		paragraph_Renamed.ApplyStyle(builtinStyle_Renamed)
	Next builtinStyle_Renamed

	'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