none
control RRS feed

  • Question

  • i have this code 

    Microsoft.Office.Tools.Word.Bookmark bookmark2 = this.Controls.AddBookmark(this.Paragraphs[2].Range,"bookmark2");
    i have this code, i want to have a substitute for "this.control", does anyone have another code to replace the "this."

    Wednesday, December 8, 2010 6:30 AM

Answers

  • Which line of code is causing the error?

    FWIW the following line of code deletes the bookmark:

    bookmark2.Range.Text = "This is sample bookmark text: ";

    You need to do something more liike:

    Word.Range rng = bookmark2.Range;
    rng.Text = "yxza";
    app.ActiveDocument.Bookmarks.Add("bookmark name", rng);


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Wednesday, December 15, 2010 7:48 AM
    Wednesday, December 8, 2010 7:11 PM
    Moderator

All replies

  • Hi Neo

    Could you please elaborate a bit more on your development environment?

    Is this a VSTO document or Add-in? Which version of VSTO? Which version of Word?

    Up until now, I've had the impression that you haven't been working with VSTO. If that's the case, then you cannot use the "Tools" namespace in your project. You can only use the Interop.

    The "this" in the code refers to the VSTO project. There is no way to "substitute" it and use the code in another type of project (WinForm, for example).


    Cindy Meister, VSTO/Word MVP
    Wednesday, December 8, 2010 6:35 AM
    Moderator
  • in fact am creating a bookmark for word 2002 plugin and i got this code from the MSDN forum http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.bookmark.insertcrossreference.aspx and the "this.control" is not working, 

    so i want a code to use instead of the "this.control"

    Wednesday, December 8, 2010 6:53 AM
  • in fact am creating a bookmark for word 2002 plugin and i got this code from the MSDN forum http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.bookmark.insertcrossreference.aspx and the "this.control" is not working, 

    so i want a code to use instead of the "this.control"


    Hi neo

    VSTO and a "Shared Add-in" are two different things. As VSTO does not support Office XP (2002), so I assume this is a "Shared Add-in". You cannot use the VSTO namespace (the "Tools") for a Shared Add-in.

    Therefore, in order to add a bookmark to a document you must use the "interop":
    using Word = Microsoft.Office.Interop.Word;

    Word.Document.Bookmarks.Add(Word.Range, string BookmarkName);


    Cindy Meister, VSTO/Word MVP
    Wednesday, December 8, 2010 7:18 AM
    Moderator
  • i tried the following

     private void BookmarkInsertCrossReference()

            {

                app.ActiveDocument.Paragraphs[1].Range.InsertParagraphBefore();

                app.ActiveDocument.Paragraphs[1].Range.InsertParagraphBefore();

     

                //Bookmark bookmark1 = this.Controls.AddBookmark(app.ActiveDocument.Paragraphs[1].Range, "bookmark1");

                Bookmark bookmark1 = app.Selection.Bookmarks.Add("bookmark1", app.ActiveDocument.Paragraphs[1].Range);

                bookmark1.Range.Text = "Heading of Document";

                //bookmark1.Range.set_Style("Heading 1");

     

     

                //Bookmark bookmark2 =

                //    app.ActiveDocument.Controls.AddBookmark(app.Selection.Paragraphs[2].Range,

                //    "bookmark2");

                Bookmark bookmark2 = app.Selection.Bookmarks.Add("bookmark2", app.ActiveDocument.Paragraphs[2].Range);

                bookmark2.Range.Text = "This is sample bookmark text: ";

     

                object ReferenceType = "Heading";

                object ReferenceItem = "1";

                object InsertAsHyperlink = true;

                object IncludePosition = false;

                object SeparateNumbers = false;

                object SeparatorString = " ";

     

                bookmark2.Range.Words.Last.InsertCrossReference(ref ReferenceType, WdReferenceKind.wdContentText, ref ReferenceItem, ref InsertAsHyperlink, ref IncludePosition, ref SeparateNumbers, ref SeparatorString);

                //app.Selection.InsertCrossReference(WdReferenceType.wdRefTypeBookmark, WdReferenceKind.wdContentText, bookmark2, true, false, false, Type.Missing);

            }

     

     

    but when am running it am getting an error saying that 

    Attempted to read or write protected memory. This often indicates that other memory is corrupt.

     

     

    do you have any idea ???

    Wednesday, December 8, 2010 7:42 AM
  • and can you tell me hows the insert cross reference works ?
    Wednesday, December 8, 2010 7:44 AM
  • Which line of code is causing the error?

    FWIW the following line of code deletes the bookmark:

    bookmark2.Range.Text = "This is sample bookmark text: ";

    You need to do something more liike:

    Word.Range rng = bookmark2.Range;
    rng.Text = "yxza";
    app.ActiveDocument.Bookmarks.Add("bookmark name", rng);


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Wednesday, December 15, 2010 7:48 AM
    Wednesday, December 8, 2010 7:11 PM
    Moderator
  • on this line am getting the error

     

     bookmark2.Range.Words.Last.InsertCrossReference(WdReferenceType.wdRefTypeBookmark, WdReferenceKind.wdContentText, ref ReferenceItem, ref InsertAsHyperlink, ref IncludePosition, ref SeparateNumbers, ref SeparatorString);

    Thursday, December 9, 2010 5:16 AM
  • That's because the bookmark no longer exists. Please see the information in my previous reply.
    Cindy Meister, VSTO/Word MVP
    Thursday, December 9, 2010 10:02 AM
    Moderator