This project has moved and is read-only. For the latest updates, please go here.

Can not export a pptx file to xps file! error!!!!

Jul 8, 2013 at 11:02 AM
Edited Jul 8, 2013 at 11:04 AM
I use this code to convert a pptx file to a xps file.but there is an error.
but word file is fine using the netoffice api !
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel = NetOffice.ExcelApi;
using PowerPoint = NetOffice.PowerPointApi;
using Word = NetOffice.WordApi;


using ExampleBase;

using NetOffice;

using Office = NetOffice.OfficeApi;
using NetOffice.WordApi.Enums;
using NetOffice.OfficeApi.Enums;
using NetOffice.PowerPointApi.Enums;
using System.Reflection;
private static OfficeToXpsConversionResult ConvertFromPowerPoint(string sourceFilePath, ref string resultFilePath)
    {
        string pSourceDocPath = sourceFilePath;
        string pExportFilePath = string.IsNullOrEmpty(resultFilePath) ? GetTempXpsFilePath() : resultFilePath;
        try
        {
            PowerPoint.Application pptApplication =null;
            PowerPoint.Presentation  pptPresentation = null;
            try
            {
                pptApplication = new PowerPoint.Application();
            }
            catch (Exception exc)
            {
                return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToInitializeOfficeApp, "PowerPoint", exc);
            }

            try
            {
                try
                {
                    pptPresentation = pptApplication.Presentations.Open(pSourceDocPath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
                }
                catch (Exception exc)
                {
                    return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile, exc.Message, exc);
                }

                if (pptPresentation != null)
                {
                    try
                    {
                     pptPresentation.ExportAsFixedFormat(pExportFilePath,
                       NetOffice.PowerPointApi.Enums.PpFixedFormatType.ppFixedFormatTypeXPS,
                       NetOffice.PowerPointApi.Enums.PpFixedFormatIntent.ppFixedFormatIntentScreen,
                       NetOffice.OfficeApi.Enums.MsoTriState.msoFalse,
                       NetOffice.PowerPointApi.Enums.PpPrintHandoutOrder.ppPrintHandoutVerticalFirst,
                       NetOffice.PowerPointApi.Enums.PpPrintOutputType.ppPrintOutputSlides,
                       NetOffice.OfficeApi.Enums.MsoTriState.msoFalse,
                       null,
                       NetOffice.PowerPointApi.Enums.PpPrintRangeType.ppPrintAll,
                       string.Empty,
                       false,
                       true,
                       false,
                       true,
                       false,
                       Missing.Value
                       );

                    //pptPresentation.ExportAsFixedFormat2(
                    //                    pExportFilePath,
                    //                    PpFixedFormatType.ppFixedFormatTypeXPS,
                    //                    PpFixedFormatIntent.ppFixedFormatIntentScreen,
                    //                    MsoTriState.msoFalse,
                    //                    PpPrintHandoutOrder.ppPrintHandoutVerticalFirst,
                    //                    PpPrintOutputType.ppPrintOutputSlides,
                    //                    MsoTriState.msoFalse,
                    //                    null, PpPrintRangeType.ppPrintAll,
                    //                    string.Empty,
                    //                    false,
                    //                    true,
                    //                    false,
                    //                    true,
                    //                    false
                    //                );

                    }
                    catch (Exception exc)
                    {
                        return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToExportToXps, "PowerPoint", exc);
                    }
                }
                else
                {
                    return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile);
                }
            }
            finally
            {
                // Close and release the Document object.
                if (pptPresentation != null)
                {
                    pptPresentation.Close();
                    pptPresentation = null;
                }

                // Quit Word and release the ApplicationClass object.
                if (pptApplication != null)
                {
                    pptApplication.Quit();
                    pptApplication = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
        catch (Exception exc)
        {
            return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToAccessOfficeInterop, "PowerPoint", exc);
        }

        resultFilePath = pExportFilePath;

        return new OfficeToXpsConversionResult(ConversionResult.OK, pExportFilePath);

    }
Jul 8, 2013 at 3:28 PM
what do you think i can do for you without the informations: error line, exception info, office version and so on? :o
Jul 9, 2013 at 2:57 AM
Edited Jul 9, 2013 at 3:00 AM
SebastianDotNet wrote:
what do you think i can do for you without the informations: error line, exception info, office version and so on? :o
thank you for reply
The officeversion is 2007, and the error line is
pptPresentation.ExportAsFixedFormat(pExportFilePath, ...
then turn to the catch line
    //                    false
                //                );

                }
                catch (Exception exc)
                {
                    return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToExportToXps, "PowerPoint", exc);
                }
            }
.....................................................
the same code use Microsoft.Office.Interop is fine.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Word = Microsoft.Office.Interop.Word;
private static OfficeToXpsConversionResult ConvertFromPowerPoint(string sourceFilePath, ref string resultFilePath)
    {
        string pSourceDocPath = sourceFilePath;

        string pExportFilePath = string.IsNullOrEmpty(resultFilePath) ? GetTempXpsFilePath() : resultFilePath;

        try
        {
            PowerPoint.Application pptApplication = null;
            PowerPoint.Presentation pptPresentation = null;

            try
            {
                pptApplication = new PowerPoint.Application();
            }
            catch (Exception exc)
            {
                return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToInitializeOfficeApp, "PowerPoint", exc);
            }

            try
            {
                try
                {
                    pptPresentation = pptApplication.Presentations.Open(pSourceDocPath,
                                                                        Microsoft.Office.Core.MsoTriState.msoTrue,
                                                                        Microsoft.Office.Core.MsoTriState.msoTrue,
                                                                        Microsoft.Office.Core.MsoTriState.msoFalse);
                }
                catch (Exception exc)
                {
                    return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile, exc.Message, exc);
                }

                if (pptPresentation != null)
                {
                    try
                    {
                        pptPresentation.ExportAsFixedFormat(
                                            pExportFilePath,
                                            PowerPoint.PpFixedFormatType.ppFixedFormatTypeXPS,
                                            PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentScreen,
                                            Microsoft.Office.Core.MsoTriState.msoFalse,
                                            PowerPoint.PpPrintHandoutOrder.ppPrintHandoutVerticalFirst,
                                            PowerPoint.PpPrintOutputType.ppPrintOutputSlides,
                                            Microsoft.Office.Core.MsoTriState.msoFalse,
                                            null,
                                            PowerPoint.PpPrintRangeType.ppPrintAll,
                                            string.Empty,
                                            true,
                                            true,
                                            true,
                                            true,
                                            false
                                        );
                    }
                    catch (Exception exc)
                    {
                        return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToExportToXps, "PowerPoint", exc);
                    }
                }
                else
                {
                    return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile);
                }
            }
            finally
            {
                // Close and release the Document object.
                if (pptPresentation != null)
                {
                    pptPresentation.Close();
                    pptPresentation = null;
                }

                // Quit Word and release the ApplicationClass object.
                if (pptApplication != null)
                {
                    pptApplication.Quit();
                    pptApplication = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
        catch (Exception exc)
        {
            return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToAccessOfficeInterop, "PowerPoint", exc);
        }

        resultFilePath = pExportFilePath;

        return new OfficeToXpsConversionResult(ConversionResult.OK, pExportFilePath);
    }

Jul 10, 2013 at 10:26 PM
OMG this is powerpoint bug, the method doesnt support latebinding becuse microsoft does a mistake and set the default value for the printrange parameter to 0. the method is not callable as c# dynamic or vb latebinding for this reason without a valid printrange parameter.

PowerPoint.PrintRanges ranges = pptPresentation.PrintOptions.Ranges;
PowerPoint.PrintRange range = ranges.Add(1, 1);

change null to range and it works for me.(add the last parameter)
next time, please gimme more exception info incl. innerexceptions. i can help you much faster with these info.

btw:
you dont need this scary stuff in NetOffice:
GC.Collect();
GC.WaitForPendingFinalizers();

use pptApplication.Dispose() after Quit() and thats all you have to do. no com proxy is alive any longer.

sebastian