Page tree

Versions Compared

Key

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

...

Table

...

of

...

Contents

Table of Contents
maxLevel1

Hello World with ExcelApplication

WordWriter's

...

WordApplication

...

approach

...

provides

...

you

...

with

...

full

...

programmatic

...

control

...

over

...

the

...

binary

...

Word

...

file

...

formats

...

(DOC,

...

DOT).

...

This

...

includes

...

the

...

ability

...

to

...

create

...

and

...

modify:

...

paragraphs,

...

tables,

...

lists,

...

hyperlinks,

...

bookmarks,

...

images

...

and

...

more

...

!

...

This

...

tutorial

...

will

...

show

...

you

...

how

...

to

...

create

...

a

...

new

...

document

...

and

...

write

...

some

...

text

...

to

...

that

...

document.

...

Diving

...

right

...

into

...

the

...

code

...

}
Info

In

the

Hello

World

sample

web

application,

WordApplication_HelloWorld.aspx

has

a

text

box

for

users

to

supply

a

value

and

WordApplication_HelloWorld.aspx.cs/vb

contains

the

code

show

below.

{info} 1. Include the {{

  1. Include the SoftArtisans.OfficeWriter.WordWriter

...

  1.  namespace in

...

  1. the

...

  1. code

...

  1. behind.

...

  1. Code Block
    languagec#
    using SoftArtisans.OfficeWriter.WordWriter;

...

  1. Code Block
    languagevb
    Imports SoftArtisans.OfficeWriter.WordWriter

...



  1. Instantiate the WordApplication object

    Code Block
    languagec#
    WordApplication WAPP = new WordApplication();

...

  1. Code Block
    languagevb
    Dim WAPP As WordApplication = new WordApplication()

...

  1.  

     

  2. Create a new document with WordApplication.Create()

...

  1. Code Block
    languagec#
    Document DOC = WAPP.Create();

...

  1. Code Block
    languagevb
    Dim DOC As Document = WAPP.Create()

...

  1.  

    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.

    Note

    WordApplication only supports the Office 2003 file formats - DOC, DOT. WordWriter does not currently support creating DOCX/DOCM files with WordApplication.

  2. For this example, we'll pull a value from a text box in a web form. Get the value.

    Code Block
    languagec#
    string value = DataValueBox.Text.Trim();

...

  1. Code Block
    languagevb
    Dim value As string = DataValueBox.Text.Trim()

...



  1. Insert the text into the document with Element.InsertTextAfter.

    Code Block
    languagec#
    DOC.InsertTextAfter(value, true);

...

  1. Code Block
    languagevb
    DOC.InsertTextAfter(value, true)

...

  1. 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.



  2. Save the output file with WordApplication.Save

    Code Block
    languagec#
    WAPP.Save(DOC, Response, "Output.doc", false);

...

  1. Code Block
    languagevb
    WAPP.Save(DOC, Response, "Output.doc", false)

...

  1. WordApplication.Save

...

  1. has

...

  1. the

...

  1. same

...

  1. output

...

  1. options

...

  1. as

...

  1. WordTemplate:

...

  1. save

...

  1. to

...

  1. disk,

...

  1. save

...

  1. to

...

  1. memory

...

  1. stream,

...

  1. stream

...

  1. back

...

  1. to

...

  1. the

...

  1. client

...

  1. inline,

...

  1. and

...

  1. stream

...

  1. back

...

  1. to

...

  1. the

...

  1. client

...

  1. as

...

  1. an

...

  1. attachment.

...

  1. In

...

  1. this

...

  1. case,

...

  1. we're

...

  1. streaming

...

  1. the

...

  1. document

...

  1. back

...

  1. to

...

  1. the

...

  1. client

...

  1. as

...

  1. an

...

  1. attachment.

...

  1. Here

...

  1. is

...

  1. an

...

  1. example

...

  1. of

...

  1. what

...

  1. the

...

  1. output

...

  1. should

...

  1. look

...

  1. like:

    Image Added

     

Congratulations, you have completed Hello World using WordApplication!

Section
Column
width50
Code Block
languagec#
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);
{csharp} {vbnet:7}
Column
width50
Code Block
languagevb
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)
{vbnet} h1. Downloads You can download the code for the hello world tutorial as a visual studio solution. * [Hello World Tutorial^WordWriter

Downloads

You can download the code for the Hello World tutorial as a Visual Studio solution.

...