none
foreach loop not working if I am trying to Delete shape RRS feed

  • Question

  • So I have 2 shape in my document.

    oWord.ActiveDocument.Shapes.Count = 2

    So I am expecting code inside the for loop will execute 2 times as I have 2 shapes. 

    Can anyone tell me why the foreach loop does not work if I use shape.Delete(). 

    This code, after first time of the loop it delete the first shape and it breaks the for each loop. So my second shape is not deleting. 

    using word = Microsoft.Office.Interop.Word;					
    private void btnOK_Click(object sender, EventArgs e)
            {
                Microsoft.Office.Interop.Word._Application oWord;
                object oMissing = Type.Missing;            
                oWord = Globals.ThisAddIn.Application;
                if (oWord.ActiveDocument.Shapes.Count > 0)
                        {
                            foreach (word.Shape shape in oWord.ActiveDocument.Shapes)
                            {                            
                                shape.Delete();                                                                                        
                            }
                        }
    		}					

    But if I use something else like this, it goes through 2 times and change the text of each shape. 

    using word = Microsoft.Office.Interop.Word;					
    private void btnOK_Click(object sender, EventArgs e)
            {
                Microsoft.Office.Interop.Word._Application oWord;
                object oMissing = Type.Missing;            
                oWord = Globals.ThisAddIn.Application;
                if (oWord.ActiveDocument.Shapes.Count > 0)
                        {
                            foreach (word.Shape shape in oWord.ActiveDocument.Shapes)
                            {                            
                                //shape.Delete();                                
                                shape.TextFrame.TextRange.Text = "";                            
                            }
                        }
    		}

    What am I missing here so shape.Delete() is breaking my for loop ?

    Friday, April 20, 2018 1:19 PM

Answers

  • Hello,

    If you delete the object in order, the second shape will be first shape while you deleted first shape and so it will not be deleted. I would suggest you try to delete the shapes in a reverse order.

    Here is the example.

    if (oWord.ActiveDocument.Shapes.Count > 0)
                {
                    for (int i = oWord.ActiveDocument.Shapes.Count; i>0; i--) {
                        oWord.ActiveDocument.Shapes[i].Delete();
                    }
                }

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by newbiesaspnet Monday, April 23, 2018 1:20 PM
    Monday, April 23, 2018 2:20 AM