Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin
Excerpt Include
WordTemplate in depth
WordTemplate in depth
nopaneltrueWordTemplate in depth

Tabled of Contents

Table of Contents

...

Pass the Save method a file path, for example:

Code Block
c#
c#
titleC#c#
WordTemplate wt = new WordTemplate();
wt.Process();
wt.Save(@"c:\Documents\File.doc");
Code Block
vb.net
vb.net
titleVB.NETvb.net
Dim wt As New WordTemplate()
wt.Process()
wt.Save("c:\Documents\File.doc")

...

Pass the Save method a System.IO.FileStream object, for example:

Code Block
c#
c#
titleC#c#
using System.IO; // For FileStream object
WordTemplate wt = new WordTemplate();
wt.Process();
using(FileStream fstream = new FileStream(@"c:\Documents\File.doc",
		FileMode.Create))
{
	wt.Save(fstream);
	fstream.Close();
}
Code Block
vb.net
vb.net
titleVB.NETvb.net
Imports System.IO ' For FileStream object
Dim wt As New WordTemplate()
wt.Process()
Dim fstream As new FileStream("c:\Documents\File.doc",
		FileMode.Create)
Try
	wt.Save(fstream)
	fstream.Close()
Finally
	If Not fstream Is Nothing Then
		fstream.Dispose()
	End If
End Try

...

When you pass an HttpResponse object to Save, WordWriter will stream the generated Word file to the client. The browser will display a File Download dialog asking the user to open or save the file. The method's second parameter specifies a file name to display in the File Download dialog. If the method's third parameter - OpenInBrowser - is True and the user chooses to open the Word file, the file will open in the browser window.

Code Block
c#
c#
titleC#c#
wt.Save(Page.Response, "StringVarOutput.doc", true);
Code Block
vb.net
vb.net
titleVB.NETvb.net
wt.Save(Page.Response, "StringVarOutput.doc", True)

...

When you pass an HttpResponse object to Save, WordWriter will stream the generated Word file to the client. The browser will display a File Download dialog asking the user to open or save the file. The method's second parameter specifies a file name to display in the File Download dialog. If the method's third parameter - OpenInBrowser - is False and the user chooses to open the Word file, the file will open in the default application for .doc or .docx files (such as Microsoft Word, StarOffice, or OpenOffice.org).

Code Block
c#
c#
titleC#c#
bool bOpenInBrowser = false;
wt.Save(Page.Response, "StringVarOutput.doc", bOpenInBrowser);
Code Block
vb.net
vb.net
titleVB.NETvb.net
Dim bOpenInBrowser As Boolean = False
wt.Save(Page.Response, "StringVarOutput.doc", bOpenInBrowser)

...

  1. Set the response's content-type header to "application/vnd.ms-word":
    Code Block
    c#
    c#
    titleC#c#
    Response.ContentType = "application/vnd.ms-word";
    
    Code Block
    vb.net
    vb.net
    titleVB.NETvb.net
    Response.ContentType = "application/vnd.ms-word"}}
    
  2. Set the response's content-disposition to "attachment":
    Code Block
    c#
    c#
    titleC#c#
    Response.AddHeader("Content-Disposition",
         "attachment;filename=/"WordWriter.doc/"");
    
    Code Block
    vb.net
    vb.net
    titleVB.NETvb.net
    Response.AddHeader("Content-Disposition",\_
         "attachment;filename=""WordWriter.doc""")
    
  3. Pass the Save method Response.Stream as a parameter:
    Code Block
    c#
    c#
    titleC#c#
    wt.Save(Response.OutputStream);
    
    Code Block
    vb.net
    vb.net
    titleVB.NETvb.net
    wt.Save(Response.OutputStream)
    

...

Code Block
c#
c#
Document doc WordApplication.Open(WordTemplate template);

Example

Code Block
c#
c#
titleC#c#
WordTemplate wTempl = new WordTemplate();
wTempl.Process();

WordApplication wApp = new WordApplication();
Document doc = wApp.Open(wTempl);
Code Block
vb.net
vb.net
titleVB.NETvb.net
Dim wTempl As New WordTemplate
wTempl.Process()

Dim wApp As New WordApplication
Dim doc As Document = wApp.Open(wTempl)