Word Document Bookmarks
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.interop.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Andrew Thompson via .NET 247 (VIP)
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

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>qqyT1HV7aEiqHQo9cWXoFQ==</Id>
Reply to this message...
 
    
BBFrost
Andrew,

I'm using the Office XP PIA's
VS Net 2003
..Net Framework 1.1
Office XP (2002)

The following method is working for me. It was derived with a lot of help
from this group and a number of links including ...

http://www.microsoft.com/downloads/details.aspx?FamilyId=8B2A7288-BD98-401D-
B309-2E587D98CD67&displaylang=en
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/h
tml/wroriautomatingexcelusingexcelobjectmodel.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto200
3_ta/html/WordObject.asp

Hope it works for your purposes.

//'---------------------------------------------------------
//' METHOD: 01/29/04
//'---------------------------------------------------------

private void ReplaceBookmarkText(Word._Document pMsWordDoc, string
pBookmarkName, string pNewText)
{
object missingValue = Type.Missing;
object oBookmarkName = pBookmarkName;

try
{
Word.Range rng = pMsWordDoc.Bookmarks.Item(ref
oBookmarkName).Range;
rng.Text = pNewText;
rng.Font.Bold = 0;
rng.Font.Color = Word.WdColor.wdColorBlue;
object oRng = rng;
pMsWordDoc.Bookmarks.Add(pBookmarkName, ref oRng);
}
catch
{
string sMsg = "\nUnknown or Unhandled bookmark encountered:
" + pBookmarkName + "\n\n";
MessageBox.Show(sMsg.ToString(),"Problem with a
bookmark:",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}

Best wishes,

Barry
in Oregon

"Andrew Thompson via .NET 247" <Click here to reveal e-mail address> wrote in message
news:#Click here to reveal e-mail address...
[Original message clipped]

include an Item definition.
[Original message clipped]

Reply to this message...
 
 
System.Reflection.Missing
System.Type
System.Windows.Forms.MessageBox
System.Windows.Forms.MessageBoxButtons
System.Windows.Forms.MessageBoxIcon




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com