Problems with Word Automation in ASP.NET

I have the below code. I am trying to insert some text into a predefined MSWord template that has a table in it. In my project I have references to the following  .dlls. (Microsoft.Office.Core.dll, Microsoft.Office.Interop.Word.dll) I get the following error when trying to refer to the cells of the table:

"Microsoft.Office.Interop.Word.Tables' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'Microsoft.Office.Interop.Word.Tables' could be found (are you missing a using directive or an assembly reference?)"

This seems like I am missing an assembly reference but I have done a lot of reading on the dlls needed for automation in and it is kind of confusing in trying to discern what it is I need. Office 2003 is installed on my local where I am running this from. Any advice would be greatly appreciated.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Runtime;
using System.Data;
using Word = Microsoft.Office.Interop.Word;
namespace CodeProjectAutomation
    public partial class _Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
        protected void Button1_Click(object sender, EventArgs e)
            // Handle parameters you don't care about in .NET        
            object oMissing = System.Reflection.Missing.Value;
            // Predefined bookmark
            object oEndOfDoc = "\\endofdoc";
            // Launch MS Word
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            // Get the document template (.DOT) file
            object objDocTemplate = Server.MapPath("") + "\\MyTemplate\\";
            oDoc = oWord.Documents.Add(ref objDocTemplate, ref oMissing, ref oMissing,
                ref oMissing);
            // Forcefully hide the table GridLines
            oDoc.ActiveWindow.View.TableGridlines = false;
            // Forcefully set the Zoom percentage to 100% when opening the document
            oDoc.ActiveWindow.ActivePane.View.Zoom.Percentage = 100;
            // Forcefully hide the Spelling Errors
            oDoc.ShowSpellingErrors = false;
            // Add text data to the cells in the Table (This can be static data or 
            // data retrieved from database)
            oDoc.Tables.Item(1).Cell(1, 1).Range.Text = "My Document's Title";
            oDoc.Tables.Item(1).Cell(2, 1).Range.Text = "My Document Description";
            oDoc.Tables.Item(1).Cell(3, 1).Range.Text = "My Image Name";
            // Save opened template as another .doc file
            Object oSaveAsFile = Server.MapPath("") + "\\MyTemplate\\MyDocument.doc";
            oDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing);
            // Close the document, destroy the object and Quit Word
            object SaveChanges = true;
            oDoc.Close(ref SaveChanges, ref oMissing, ref oMissing);
            oDoc = null;
            oWord.Quit(ref SaveChanges, ref oMissing, ref oMissing);

Open in new window

Who is Participating?
rambovnConnect With a Mentor Commented:
Try get_Item. I think that's fairly standard when automating Office apps
from C#:

wd.Range rng = wdDoc.Bookmarks.get_Item(ref objBkmName).Range;

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.