访问PowerPoint加载项中的幻灯片对象

时间:2012-10-02 09:57:46

标签: c# powerpoint

我正在构建PowerPoint的加载项,需要访问幻灯片或幻灯片对象,甚至是整个演示文稿;唉,我能看到的唯一方法就是打开 new ppt文件。现在,我不得不求助于保存当前演示文稿的hacky方法,并使用Packaging重新打开它来操作任何东西(更具体地说,我需要从pptx文件中删除SHA对象以查看它们是否已更改 - 不理想)

有没有办法打开当前在PowerPoint中打开的文件而不必IO文件?

感谢您的帮助, P

1 个答案:

答案 0 :(得分:0)

我假设您已在VisualStudio中创建了PowerPoint(2007/2010)加载项项目。通常,您可以通过以下方式使用静态类Globals访问活动演示文稿:

Globals.ThisAddIn.Application.ActivePresentation.Slides[slideIndex] ...

编辑:用法示例:

using PowerPoint = Microsoft.Office.Interop.PowerPoint;

...

try
{
    int numberOfSlides = Globals.ThisAddIn
        .Application.ActivePresentation.Slides.Count;

    if (numberOfSlides > 0)
    {
        // get first slide
        PowerPoint.Slide firstSlide = Globals.ThisAddIn
            .Application.ActivePresentation.Slides[0];

        // get first shape (object) in the slide
        int shapeCount = firstSlide.Shapes.Count;

        if (shapeCount > 0)
        {
            PowerPoint.Shape firstShape = firstSlide.Shapes[0];
        }

        // add a label
        PowerPoint.Shape label = firstSlide.Shapes.AddLabel(
                Orientation: Microsoft.Office.Core
                   .MsoTextOrientation.msoTextOrientationHorizontal,
                Left: 100,
                Top: 100,
                Width: 200,
                Height: 100);

        // write hello world with a slidenumber
        label.TextFrame.TextRange.Text = "Hello World! Page: ";
        label.TextFrame.TextRange.InsertSlideNumber();
    }
}
catch (Exception ex)
{
    System.Windows.Forms.MessageBox.Show("Error: " + ex);

}