Solved

Word Document Bookmarks

Posted on 2004-08-21
4
358 Views
Last Modified: 2012-05-05
Hi,
 
I am working on a project where I need to automate MS Word and create some Documents.

I have no problems creating a word application or document object, but I do have a problem wtih the Bookmarks.

I have the following code:

                  object oMissing = System.Reflection.Missing.Value;
                  object oEndOfDoc = "\\endofdoc";
                  object oTemplate = "C:\\temp\\Invoice.dot";
                  object oBookMark = "Name";
                  
                  Word._Application oWord;
                  Word._Document oDoc;

                  oWord = new Word.Application();
                  
                  oWord.Visible = true;

                  oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);

                  oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "TEXT GOES HERE";

However when I build my project, I get the following error:

'Word.Bookmarks' does not contain a definition for 'Item'

Now on all the examples I have seen the above Bookmark code is used, but why do I get that error ? The Intellisense menu VS.Net pops up does not include an Item definition.

Am I not referencing something correctly ?

Thanks

Andrew
0
Comment
Question by:ttnetworks
4 Comments
 
LVL 10

Expert Comment

by:123654789987
ID: 11868484
I got a piece of code from MSDN for updating a BookMark text. The code is

internal void BookMarkReplaceRange(string bookmarkName, string newText)
{
    object missingValue = Type.Missing;
    object oBookmarkName = bookmarkName;
    Word.Range rng = ThisDocument.Bookmarks.get_Item(    //Here we are getting the range from an existing Bookmark.
        ref oBookmarkName).Range;                                        // Then we are reseting the text.
    rng.Text = newText;
    object oRng = rng;
    ThisDocument.Bookmarks.Add(bookmarkName, ref oRng);
}

You can refer the following article for more details

http://support.microsoft.com/default.aspx?scid=kb;EN-US;316384

Here they have asked u to add the following on top of the page

using Word = Microsoft.Office.Interop.Word;
using System.Reflection;



0
 

Author Comment

by:ttnetworks
ID: 11868510
Hi,

Yes I have read that article already, though it doesnt help much because when I compile the program I am told that Bookmarks does not contain a definition for get_Item. Which is the same error I get when using Bookmark.Item.

The using statments are already added to the class.

Andrew
0
 
LVL 4

Accepted Solution

by:
thedude112286 earned 500 total points
ID: 12011771
Try this:

...
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
oDoc.Activate();
...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

13 Experts available now in Live!

Get 1:1 Help Now