Word Template

  • Thread starter Thread starter Ruslan Shlain
  • Start date Start date
R

Ruslan Shlain

Hello everyone.
I have a question that I tried to find an answer over the weekend but did
not succeeded.
I have a Word template with a couple of "tags" ( <<My Tag>>) that I need to
replace with data from the DB. Please point me in the right direction on how
to approach this. Code samples, links or articles. Anything would work.

Thank you very much in advance.
 
Ruslan,

In order to get data from the database, you are going to have to use the
classes in one of the following namespaces:

System.Data.SqlClient
System.Data.OleDb
System.Data.Odbc

Which you use is dependent on the datbase technology you are using. If
you have a different provider that you need to use, you can use that.

To access Word in .NET, you will have to automate it. In order to do
that, check out the knowledge base article titled "HOW TO: Automate Word
from Visual C# .NET to Create a New Document", located at (watch for line
wrap):

http://support.microsoft.com/default.aspx?scid=kb;EN-US;316384

While this example shows how to perform rudimentary operations in Word
from C#, it shows you how to interact with it, which you can use as a base
for whatever you are trying to do.

Hope this helps.
 
EX:

object nRef = Type.Missing;


object falseRef = false;

object trueRef = true;

object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */


try

{

word = new Word.Application();

word.Visible = false;

object path = this.templateDirectory + this.ClientTemplate;


Word._Document dc = word.Documents.Add(ref path,ref nRef,ref nRef,ref
trueRef);


//Date

object dDisplay = "Date";

dc.Bookmarks.Item(ref dDisplay).Range.Text = this.DateDisplayed;


//App names

//First name

object fName = "FirstName";

dc.Bookmarks.Item(ref fName).Range.Text = this.FirstName;

object lName = "LastName";

dc.Bookmarks.Item(ref lName).Range.Text = this.LastName;

object addr = "Address";

dc.Bookmarks.Item(ref addr).Range.Text = this.Address;

object aCity = "City";

dc.Bookmarks.Item(ref aCity).Range.Text = this.City;

object aState = "State";

dc.Bookmarks.Item(ref aState ).Range.Text = this.State;

object aZip = "Zip";

dc.Bookmarks.Item(ref aZip).Range.Text = this.Zip;

object openName = "OpenName";

dc.Bookmarks.Item(ref openName).Range.Text = this.FirstName;

object openNameLast = "OpenNameLast";

dc.Bookmarks.Item(ref openNameLast).Range.Text = this.LastName;

object sig = "Signature";

dc.Bookmarks.Item(ref sig).Range.Text = this.Signature;


///Use this name to save this file

object fileName = generateFileName();

object savePath = this.WorkingDirectory + fileName;

dc.SaveAs(ref savePath,ref nRef,ref nRef,ref nRef,ref falseRef,ref nRef,ref
nRef,

ref nRef,ref trueRef,ref nRef,ref nRef);



return savePath.ToString();

}

catch (Exception ex)

{

this.lastError = ex.ToString();

}

finally

{

if (word != null)

word.Quit(ref falseRef,ref nRef,ref nRef);

}


HTH,

Alex
 
Back
Top