Solved

Insert text into .doc (with reference)

Posted on 2011-09-15
6
626 Views
Last Modified: 2013-12-17
Hi,

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()
        {
            InitializeComponent();
        }

        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

0
Comment
Question by:csharp_learner
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:andrewssd3
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
0
 

Author Comment

by:csharp_learner
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
0
 
LVL 17

Expert Comment

by:andrewssd3
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.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

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

Accepted Solution

by:
andrewssd3 earned 250 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.
0
 

Author Closing Comment

by:csharp_learner
ID: 36545871
Thanks for the help and advise.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Export DataGridView 4 38
asp.net bundle 8 36
Expando 4 33
ConsoleSql 1 14
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now