title: Creating a Word Dcoument in code behind c#
body: <p>I have used an example from microsoft on how to created a word document from code behind. I have implemented this code in a simple web part in order to test it first. I have a button that when pressed whould created the Word Document, but currently nothing happens when the button is clicked. I am not sure what I am missing here so I hope somebody could provide an insight, below is the code I currently have: (I will just list the importat parts</p>
  
  <hr>
  
  <pre><code>using Microsoft.Office.Core;
  using Microsoft.Office.Server; //for use when creatigna   writing data to a MS  
                                 //Word  Document
  using Microsoft.Office.Interop.Word; //for use when creatigna   writing data to a MS 
                                       //Word Documenr
  using Word = Microsoft.Office.Interop.Word; //for use when creatign and  writing data 
                                              //to a MS Word Document
  using System.Reflection;
  
  namespace Kemp.SP2010.Badges.Badges
  {
  public partial class BadgesUserControl : UserControl
  {
  
  
  
  
  protected void Page_Load(object sender, EventArgs e)
  {
  
  GenerateBadges.Click += new EventHandler(GenerateBadges_Click);
  
  }
  
  
  
  //button to create the badges
  void GenerateBadges_Click(object sender, System.EventArgs e)
  {
  
  
  #region Create Table in MS Word Document
  
  
  object oMissing = System.Reflection.Missing.Value;
  object oEndOfDoc = "\\endofdoc"; 
  
  //Start word and create a new document
  Word._Application oWord;
  Word._Document oDoc;
  oWord = new Word.Application();
  oWord.Visible = true;
  
  oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  
  
  //Creating  a paragraph at the beginning of the document
  Word.Paragraph oPara1;
  oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
  oPara1.Range.Text = "Heading 1";
  oPara1.Range.Font.Bold = 1;
  oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph
  oPara1.Range.InsertParagraphAfter();
  
  //Creating an 2x8 table
  Word.Table oTable;
  Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
  oTable = oDoc.Tables.Add(wrdRng, 2, 8, ref oMissing, ref oMissing);
  oTable.Range.ParagraphFormat.SpaceAfter = 6;
  
  //for loop
  int r,c;
  string strText;
  for (r = 1; r &lt;= 2; r++)
  for (c = 1; c &lt;= 8; c++)
  {
  
  strText = "r" + r +"c" + c;
  oTable.Cell(r, c).Range.Text = strText;
  
  }
  
  oTable.Rows[1].Range.Font.Bold = 1;
  oTable.Rows[1].Range.Font.Italic = 1;
  
  #endregion
  }
  }
  }
  </code></pre>
  
  <hr>
  
  <p>And here is the web part with the button</p>
  
  <pre><code>&lt;%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %&gt;
  &lt;%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral,   
  PublicKeyToken=71e9bce111e9429c" %&gt; 
  &lt;%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" 
  Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, 
  PublicKeyToken=71e9bce111e9429c" %&gt; 
  &lt;%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" 
  Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, 
  PublicKeyToken=71e9bce111e9429c" %&gt;
  &lt;%@ Register Tagprefix="asp" Namespace="System.Web.UI" 
  Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, 
  PublicKeyToken=31bf3856ad364e35" %&gt;
  &lt;%@ Import Namespace="Microsoft.SharePoint" %&gt; 
  &lt;%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" 
  Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, 
  PublicKeyToken=71e9bce111e9429c" %&gt;
  &lt;%@ Control Language="C#" AutoEventWireup="true" 
  CodeBehind="BadgesUserControl.ascx.cs" 
  Inherits="Kemp.SP2010.Badges.Badges.BadgesUserControl" %&gt;
  
  &lt;!--Button to generate the Badges in a MS Word Document--&gt;
  
  &lt;tr&gt;
  &lt;td valign="top" width="100%"&gt;
  
  &lt;asp:Button ID="GenerateBadges" runat="server" Text="Generate Badges" /&gt;
  
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <hr>
  
  <p>This shoould in theory create a Word Document with a paragraph and a table when the Button is selected. But as mentioned earlier nothing happens.</p>
  
  <p>Any sugestions will be greatly appreciated.</p>
  
  <p>Many Thanks</p>