Accessing more than 1 shape in your Document

Hi I am using the following code to access the shapes in my document.
The problem is if i have more than 1 shape in the document, although it shows the count as 2, it traverses the foreach loop only once.
Any suggestions to solve this problem will be really appreciated.
Code:-

Document doc1 = new Document(dataDir + mailMergeTemplate);
NodeCollection shapes = doc1.GetChildNodes(NodeType.Shape, true);
int i = 0;
foreach(Shape shape in shapes)
{
    if (shape.OleFormat != null)
    {
        // getting the name of the attachment from the Alternative text of the document
        string ObjectName = shape.AlternativeText;
        shape.OleFormat.Save(attchPath + ObjectName + shape.OleFormat.SuggestedExtension);
        // Removes the embedded object from the document
        shapes.Remove(shape);
        i++;
    }
}
// End of code snippet
doc1.Save(dataDir + mailMergeTemplate);

P.S. :- Also I can’t find the name of my attachment in the world file. How can I retrieve the name of the attachment ?

Hi

Thanks for your inquiry. The problem occurs because you change the collection in the foreach loop. To work the problem around try using the following code:

Node[] shapes = doc1.GetChildNodes(NodeType.Shape, true).ToArray();
int i = 0;
foreach(Shape shape in shapes)
{
    if (shape.OleFormat != null)
    {
        // getting the name of the attachment from the Alternative text of the document
        string ObjectName = shape.AlternativeText;
        shape.OleFormat.Save(attchPath + ObjectName + shape.OleFormat.SuggestedExtension);
        // Removes the embedded object from the document
        shape.Remove();
        i++;
    }
}

There is no way to get the original name of embedded file because this name is not stored in the document.
Best regards.

Thanks for the help

One more thing…Does this mean that the attachments will have a junk name ? Is there no way to assign the original name of the embedded object to the attachment?

Hi

Thanks for your inquiry. No, there is no way to assign the original name of embedded objects. The original names of embedded objects are just not stored in the document, so there is no way to get the original names.
Best regards.