none
Why does Bookmark symbol remain after image inserted programmatically into Word document? RRS feed

  • Question

  • I'm using C# to insert an image into a Word 2010 document, but after insert the bookmark symbol remains visible in the Word document.

    code snippet:

    Object oRange = wordDoc.Bookmarks[i].Range;
    wordApp.Selection.InlineShapes.AddPicture(oGraphic, oMissing, oMissing, oRrange);
    wordDoc.SaveAs(newDocument);
    wordApp.Documents.Open(newDocument);

    The image inserts just AFTER the bookmark symbol, instead of replacing it.  I'm new to VSTO and Word automation so not sure how to resolve this.
    Friday, June 21, 2013 9:22 PM

Answers

  • Hi jabberpunch

    I?m not sure why showing hidden bookmarks helps with your problem but...

    If you dont want to have the bookmark in the document after inserting the image try wordDoc.Bookmarks[i].Delete()


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by jabberpunch Saturday, June 22, 2013 3:01 PM
    Saturday, June 22, 2013 10:16 AM
    Moderator

All replies

  • Hello jabberpunch,

    I believe it has to do with the "End" of the bookmark range.  So when you are inserting your picture you are inserting it after the bookmark.  So try this instead (untested code):

    Object oRange = new Range(wordDoc.Bookmarks[i].Range.Start, wordDoc.Bookmarks[i].Range.End - 1);



    Thank you,

    Nick Metnik

    Please mark my response as helpful if it has helped you in any way or as the answer if it is a valid solution.
    Blog
    LinkedIn

    Friday, June 21, 2013 9:28 PM
  • Thanks for the reply, I receive an error when I try to instantiate a new Range object:  "Cannot create an instance of the abstract class or interface 'Microsoft.Office.Interop.Word.Range'

    What you said makes sense, I'll have to explore Bookmark.Range property a little further.

    Friday, June 21, 2013 9:36 PM
  • A painfully simple solution:

    wordDoc.Bookmarks.ShowHidden = true;

    • Marked as answer by jabberpunch Friday, June 21, 2013 9:55 PM
    • Unmarked as answer by jabberpunch Saturday, June 22, 2013 2:57 PM
    Friday, June 21, 2013 9:55 PM
  • Those are the best ones!  Happy you found a solution, have a great weekend!

    Thank you,

    Nick Metnik

    Please mark my response as helpful if it has helped you in any way or as the answer if it is a valid solution.
    Blog
    LinkedIn

    Friday, June 21, 2013 10:07 PM
  • Hi jabberpunch

    I?m not sure why showing hidden bookmarks helps with your problem but...

    If you dont want to have the bookmark in the document after inserting the image try wordDoc.Bookmarks[i].Delete()


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by jabberpunch Saturday, June 22, 2013 3:01 PM
    Saturday, June 22, 2013 10:16 AM
    Moderator
  • Yeah...about that...it didn't help, I was confused.  But you're suggestion is spot on, just what I was looking for...thanks Cindy!

    Saturday, June 22, 2013 3:01 PM