A Dynamic Word Template that Generates information from SQL

By | July 27, 2010

Have you ever wondered how to automatically generate word templates that grabs information from a database?  Now why would you do that?  In case you have an organization that has a lot of departments or even branches and you want to distribute templates once and manage the data within from a SQL server database like the logo information, address information, or even contact details.  This means any change in the information of any branch or department would not entail in updating all of the templates that are in each workstation but rather change it in the database and let the template grab that information on a real time basis.

Having said that you can have one template called TestTemplate.dotx and lets say depends on your AD Organizational Unit the contents of that template would change.  For example your AD OU Membership is in “Branch A” then it would get information on a SQL Server Database regarding that Branch.


So lets start on how this is achieved.

First you need to create an Office 2007 Word Template Visual Studio Project

It will then immediately ask for you to create a new document.

Then once its created open in Design Mode ThisDocument.cs and start designing your document like you are desiging a form or a webpage.


Add the following objects in the header of the document, a Picture Content Control and Rich Text Content Control.

Name your objects according to your liking, that will be the name property that you will access on the code behind.

Now lets start coding.  Here are the codes

public static string sSQLConnectionString = "Data Source={SERVERNAME};Initial Catalog={DatabaseName};User Id={UserName};Password={Password};";

private void ThisDocument_Startup(object sender, System.EventArgs e)
{
SqlConnection myConnection;
SqlDataReader myReader;
SqlCommand myCommand;

myConnection = new SqlConnection(sSQLConnectionString);

myConnection.Open();

SqlParameter myParam = new SqlParameter("@OU", SqlDbType.NVarChar, 50);
myParam.Value = GetUserOU(sUserName);  //This is my own AD Method to get Users OU

myCommand = new SqlCommand("SELECT TOP 1 LogoPath, BranchName FROM Branches where OU = @OU", myConnection);
myCommand.Parameters.Add(myParam);

myReader = null;

myReader = myCommand.ExecuteReader();

if (myReader.HasRows)
{
 while (myReader.Read())
 {
 imgHeaderLogo.Image = System.Drawing.Image.FromFile(myReader["LogoPath"].ToString());
 txtHeaderText.Text = myReader["BranchName"].ToString();
 }
}
}

Now if you are using SQL Server like me dont forget to import these references:

using System.Data.Sql;
using System.Data.SqlClient;

Now publish your application

Then remember to set up your application on your target machine then you can start using your template. Now depends on where you published the application the setup.exe would show.

Note: The template will not run on a machine where the package was not installed as the references that are needed by the template are in that setup package.  You can also set updates automatically on the Properties –> Publish section of the project.

Heres my Final Output:


One thought on “A Dynamic Word Template that Generates information from SQL

Leave a Reply