none
VSTO: How to delete a InlineShape via C# code? RRS feed

  • Question

  • Hi All,

    I have add a picture to my word document like this way.

    Word.Range range = _doc.Application.Selection.Range;
    // Add bookmarks
    string bookmarkName = string.Format("bm_{0}_S", guid.ToString("N"));
    _doc.Application.Selection.Bookmarks.Add(bookmarkName);

    Word.Shape shape = bookmark.Range.InlineShapes.AddPicture(@"Resources\Images\Start.png").ConvertToShape();

    with the code above, I can insert a picture in my document. And i will insert a lot of pictures in my word document.

    After that, I want to delete the InlineShapes,

    I tried this way, I got the relevant Bookmark first, such as:

    Word.Bookmarks bookmarks = _doc.Application.ActiveDocument.Bookmarks

    bookmarks[index].Range.InlineShapes[0].Delete();

    I have tried inlineshapes index such as 0,1,2. But it always thrown The requested member of the collection does not exist.

    How can I delete the specific InlineShape?

    Thanks a lot,
    Monday, January 5, 2015 7:53 AM

Answers

  • Hi Ricky,

    This code works fine for me to remove the Picture InlineShape:

    Word.Document _doc;
            string bookmarkName;
            private void btnAdd_Click(object sender, RibbonControlEventArgs e)
            {
                _doc = Globals.ThisAddIn.Application.ActiveDocument;
                Word.Range range = _doc.Application.Selection.Range;
                // Add bookmarks
                bookmarkName = string.Format("bm_{0}_S", Guid.NewGuid().ToString("N"));
                Word.Bookmark bookmark = _doc.Application.Selection.Bookmarks.Add(bookmarkName);
    
                bookmark.Range.InlineShapes.AddPicture(@"C:\Start.png");
                
            }
    
            private void btnRemove_Click(object sender, RibbonControlEventArgs e)
            {
                Word.InlineShapes shapes = _doc.InlineShapes;
                foreach (Word.InlineShape shape in shapes)
                {
                    if (shape.Type == WdInlineShapeType.wdInlineShapePicture)
                    {
                        shape.Delete();
                    }
                }
            }
    I guess that, the Word application does not store the InlineShapes in the Bookmark.Range, but in the Document's InlineShapes. Any way you could get around this problem with just a little change.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, January 6, 2015 9:07 AM
    Moderator
  • Thank You Caillen,

    I have walkaround this issue by host the image in a control. And then I delete the control via Code. Anyway Thanks a lot.

    Tuesday, January 6, 2015 9:31 AM

All replies

  • Update:

    I test that,

    bookmarks[index].Range.InlineShapes.count is always = 0. It's very odd. I can use bookmarks[index].Delete() to remove the relevant Bookmark.

    Monday, January 5, 2015 8:05 AM
  • Anyone know?
    Tuesday, January 6, 2015 5:41 AM
  • Hi Ricky,

    This code works fine for me to remove the Picture InlineShape:

    Word.Document _doc;
            string bookmarkName;
            private void btnAdd_Click(object sender, RibbonControlEventArgs e)
            {
                _doc = Globals.ThisAddIn.Application.ActiveDocument;
                Word.Range range = _doc.Application.Selection.Range;
                // Add bookmarks
                bookmarkName = string.Format("bm_{0}_S", Guid.NewGuid().ToString("N"));
                Word.Bookmark bookmark = _doc.Application.Selection.Bookmarks.Add(bookmarkName);
    
                bookmark.Range.InlineShapes.AddPicture(@"C:\Start.png");
                
            }
    
            private void btnRemove_Click(object sender, RibbonControlEventArgs e)
            {
                Word.InlineShapes shapes = _doc.InlineShapes;
                foreach (Word.InlineShape shape in shapes)
                {
                    if (shape.Type == WdInlineShapeType.wdInlineShapePicture)
                    {
                        shape.Delete();
                    }
                }
            }
    I guess that, the Word application does not store the InlineShapes in the Bookmark.Range, but in the Document's InlineShapes. Any way you could get around this problem with just a little change.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, January 6, 2015 9:07 AM
    Moderator
  • Thank You Caillen,

    I have walkaround this issue by host the image in a control. And then I delete the control via Code. Anyway Thanks a lot.

    Tuesday, January 6, 2015 9:31 AM