Word Hello World in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to write a "Hello World" in a Word document.

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

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

    //Append Text
    paragraph.AppendText("Hello World!");

    //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 paragraph
	Dim paragraph_Renamed As Paragraph = document_Renamed.AddSection().AddParagraph()

	'Append Text
	paragraph_Renamed.AppendText("Hello World!")

	'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