使用powerpoint interop时无法设置SlideSize属性

时间:2017-02-01 12:55:27

标签: c# interop powerpoint office-interop com-interop

我们正在尝试使用Powerpoint Office Interop将PPT文件转换为PDF。我们正在使用ExportAsFixedFormat()进行转换,如下面的代码段所示:

        public static void ConvertPowerPointToPdf(string inputFile)
        {
            string outputFileName = @"C:\All format files\PPT2PDF.pdf";

            Microsoft.Office.Interop.PowerPoint.Application powerPointApp =
            new Microsoft.Office.Interop.PowerPoint.Application();
            Presentation presentation = null;
            Presentations presentations = null;
            try
            {
                presentations = powerPointApp.Presentations;
                presentation = presentations.Open(inputFile, MsoTriState.msoFalse, MsoTriState.msoFalse,
                MsoTriState.msoFalse);

                presentation.PageSetup.SlideSize = PpSlideSizeType.ppSlideSizeA4Paper; //It throws the exception here

                presentation.ExportAsFixedFormat(outputFileName, PpFixedFormatType.ppFixedFormatTypePDF,
                PpFixedFormatIntent.ppFixedFormatIntentPrint);
            }
            catch (Exception)
            {
                throw;
            }
        }

如果我们不设置SlideSize属性,上面的代码可以正常工作。在我们尝试设置SlideSize属性的那一刻,异常被抛出为“PageSetup(未知成员):失败”。错误消息的屏幕截图如下所示:

enter image description here

Microsoft.Office.Interop.PowerPoint 的版本为15.0.0.0, Microsoft Office 15.0对象库用作Office核心库。我的PC在Windows 8.1和我使用Microsoft Office 2013.由于我们需要自定义输出格式,我们需要设置当前抛出异常的SlideSize属性。

1 个答案:

答案 0 :(得分:0)

您拥有的代码是正确的。我能够完全重新创建您的错误,并将演示文稿标记为“最终”。尝试添加:

presentation.Final = false;

在调整大小之前添加它。希望能帮助到你。祝你好运。

相关问题