在C#中复制PPT形状的对象值

时间:2017-02-28 13:33:22

标签: c# office-interop

是否有可能创建一个PPT形状列表并让它们在从幻灯片中删除原始文件后保持其值?

我想在幻灯片中创建所有形状的列表,操纵幻灯片,但仍保留原始信息中的所有信息。

List<Microsoft.Office.Interop.PowerPoint.Shape> pptShapes = new List<Microsoft.Office.Interop.PowerPoint.Shape>();
for (int jCurr = 1; jCurr <= slide.Shapes.Count; jCurr++)
                {                        
                    currShp = slide.Shapes[jCurr];
                    pptShapes.Add(currShp);
                }
//Do something else like delete a few of the shapes
if (slide.Shapes[1] != null)
{
     slide.Shapes[1].Delete();
}

//Below gives me Null since I deleted the original shape
MessageBox.Show(pptShapes[0].Type.ToString());

我想保留原始形状,因为后来我打算将特殊复制/粘贴到占位符等,所以我需要的不仅仅是内容。

1 个答案:

答案 0 :(得分:0)

如果要引用要从某种集合中删除的值,无论是COM还是托管,都需要按值执行复制。在您的示例中,这将如下所示:

List<Microsoft.Office.Interop.PowerPoint.Shape> pptShapes = new List<Microsoft.Office.Interop.PowerPoint.Shape>();
for (int jCurr = 1; jCurr <= slide.Shapes.Count; jCurr++)
                {                        
                    currShp = slide.Shapes[jCurr];
                    pptShapes.Add(currShp);
                }

string deletedString = "";

if (slide.Shapes[1] != null)
{
     string deletedString = pptShapes[0].Type.ToString();
     slide.Shapes[1].Delete();
}

MessageBox.Show(deletedString);

对于参考类型,您需要执行此对象的DeepCopy。