Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Csharp
2
2
WordApplication WAPP = new WordApplication();
Vbnet
2
2

Dim WAPP As WordApplication = new WordApplication()

3. Create a new document with WordApplication.Create

Csharp
3
3
Document DOC = WAPP.Create();
Vbnet
3
3

Dim DOC As Document = WAPP.Create()

Unlike the WordTemplate object, which represents a single file, WordApplication works as a file generation engine. The WordApplication object can be used to create, open, and save multiple documents at once.

...

Csharp
4
4
string value = DataValueBox.Text.Trim();
Vbnet
4
4

Dim value As string = DataValueBox.Text.Trim()

5. Insert the text into the document with Document.InsertTextAfter

Csharp
5
5
DOC.InsertTextAfter(value, true);
Vbnet
5
5

DOC.InsertTextAfter(value, true)

Text strings need to be inserted into a paragraph, table cell, or list entry elements in a document. In this case, when the text is inserted into the document, it's actually inserted into the last paragraph in the document. Documents created by WordApplication come with a paragraph, so we do not need to create that paragraph.

...

Csharp
6
6
WAPP.Save(DOC, Response, "Output.doc", false);
Vbnet
6
6

WAPP.Save(DOC, Response, "Output.doc", false)

WordApplication.Save has the same output options as WordTemplate: save to disk, save to memory stream, stream back to the client inline, and stream back to the client as an attachment. In this case, we're streaming the document back to the client as an attachment.

...

Csharp
7
7
using SoftArtisans.OfficeWriter.WordWriter;
...
WordApplication WAPP = new WordApplication();

Document DOC = WAPP.Create();

string value = DataValueBox.Text.Trim();

DOC.InsertTextAfter(value, true);

WAPP.Save(DOC, Response, "Output.doc", false);
Vbnet
7
7

You can download the Hello World sample code (C#\ or VB\) to see the finished product.

Imports SoftArtisans.OfficeWriter.WordWriter
...
Dim WAPP As WordApplication = new WordApplication()

Dim DOC As Document= WAPP.Create()

Dim value As string = DataValueBox.Text.Trim()

DOC.InsertTextAfter(value, true)

WAPP.Save(DOC, Response, "Output.doc", false)

Downloads

You can download the code for the hello world tutorial as a visual studio solution.