[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Insert text into .doc (with reference)

Posted on 2011-09-15
Medium Priority
Last Modified: 2013-12-17

I want to insert some text into a MS word doc template file using C#.
I have followed the code on this website http://support.microsoft.com/kb/316384 but there is a compilation error that oDoc, oMissing and oDoc does not exist in the curent context.

Can any expert help me out here, thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace insertDoc
    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
            object oTemplate = "C:\\trail.doc";
            oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,ref oMissing, ref oMissing);
            object oBookMark = "insert";
            oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";


Open in new window

Question by:csharp_learner
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 17

Expert Comment

ID: 36545444
The problem is you have deleted too much out of the code sample provided in that article:  you need at least these variable definitions from the top of the code. C# requires all variables to be defined before use.

	object oMissing = System.Reflection.Missing.Value;
	object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */ 

	//Start Word and create a new document.
	Word._Application oWord;
	Word._Document oDoc;
	oWord = new Word.Application();
	oWord.Visible = true;

Open in new window

Also make sure you follow the instructions at the top of the article about adding references to the Word object model

Author Comment

ID: 36545655
thanks for the prompt reply, i have added the code plus the 2 directories.

But i have 1 single compile error left under oDoc.Bookmarks.Item

'Microsoft.Office.Interop.Word.Bookmarks' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'Microsoft.Office.Interop.Word.Bookmarks' could be found
LVL 17

Expert Comment

ID: 36545736
I think you need to use get_Item (as in the example).  Some properties take parameters in VBA which isn't possible in C# so they have created new get_... methods.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

ID: 36545787
sorry i'm not very good in C# so how can i edit the code to solve the error?
LVL 17

Accepted Solution

andrewssd3 earned 1000 total points
ID: 36545813
            oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = "Some Text Here";

Open in new window

There's a similar statement in the Microsoft article.  You can also get a lot of information from intellisanse when you're typing the code.

Author Closing Comment

ID: 36545871
Thanks for the help and advise.

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question