NetOfficeFw / NetOffice

🌌 Create add-ins and automation code for Microsoft Office applications.
MIT License
695 stars 143 forks source link

Export PowerPoint to Pdf throws exception #427

Open timsteen opened 1 month ago

timsteen commented 1 month ago

Calling presentation.ExportAsFixedFormat(path: pdfFilename,fixedFormatType: PpFixedFormatType.ppFixedFormatTypePDF); results in an exception:

NetOffice.Exceptions.MethodCOMException (0x80004005): Failed to proceed Method on PowerPoint.Presentation=>ExportAsFixedFormat. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH)) --- End of inner exception stack trace --- at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at NetOffice.Invoker.Method(ICOMObject comObject, String name, Object[] paramsArray) at NetOffice.Invoker.Method(ICOMObject comObject, String name, Object[] paramsArray) at NetOffice.CoreMethodExtensions.ExecuteMethod(Core value, ICOMObject caller, String name, Object argument1, Object argument2) at NetOffice.PowerPointApi._Presentation.ExportAsFixedFormat(String path, PpFixedFormatType fixedFormatType) at PowerPointExamplesCS4.Example01.RunExample() in C:\Users\tsteen\source\repos\NetOfficeFwTest\Program.cs:line 64

I am using VS 2019 with the NuGet package NetOfficeFw.PowerPoint 1.9.6 and there is Office 365 installed on the system.

jozefizso commented 1 month ago

Please consult the Microsoft documentation about the Presentation.ExportAsFixedFormat() method to pass the correct parameters.

timsteen commented 1 month ago

Hi Jozef,

I am migrating an existing project from using Interop assemblies to NetOfficeFw. Using the Interops the export succeeds. So I assume I have the correct parameters. I also tested with different sets of parameters. The example uses just two parameters as a minimal example.

timsteen commented 3 weeks ago

Hi Jozef, any update for this. Perhaps you have a unit test for the export functionality?

jozefizso commented 3 weeks ago

Did you use Microsoft documentation to correctly call this API?

timsteen commented 3 weeks ago

Yes I did, And as I already mentioned I am migrating an Interop project ot NetOfficeFw and with using Interop the call was successful.

timsteen commented 3 weeks ago

I did some tests with the PowerPointExamples CSharp Example02. After the line "presentation.SaveAs(documentFile);" I added the following code:

        try
        {
            string pdfFile = new FileInfo(Path.ChangeExtension(documentFile, "pdf")).FullName;
            presentation.ExportAsFixedFormat(path: pdfFile,
                                             fixedFormatType: PpFixedFormatType.ppFixedFormatTypePDF,
                                             intent: PpFixedFormatIntent.ppFixedFormatIntentPrint,
                                             frameSlides: MsoTriState.msoFalse,
                                             handoutOrder: PpPrintHandoutOrder.ppPrintHandoutHorizontalFirst,
                                             outputType: PpPrintOutputType.ppPrintOutputBuildSlides,
                                             printHiddenSlides: MsoTriState.msoFalse,
                                             printRange: null,
                                             rangeType: PpPrintRangeType.ppPrintAll,
                                             slideShowName: "Example2",
                                             includeDocProperties: MsoTriState.msoFalse,
                                             keepIRMSettings: MsoTriState.msoTrue,
                                             docStructureTags: MsoTriState.msoTrue,
                                             bitmapMissingFonts: MsoTriState.msoTrue,
                                             useISO19005_1: MsoTriState.msoFalse,
                                             //includeMarkup: MsoTriState.msoFalse,
                                             externalExporter: null
                                             );
        }
        catch (Exception ex)
        {
            HostApplication.ShowErrorDialog("Export failed", ex);
        }

The error dialog was always shown with a type mismatch exception independent of providing all parameters or only some as the method declatarions ExportAsFixedFormat(2) allow , I also tried to replace the MsoTriState values with boolean values but always the same result.

The resulting stack trace was always like at NetOffice.Invoker.Method(ICOMObject comObject, String name, Object[] paramsArray) at NetOffice.PowerPointApi._Presentation.ExportAsFixedFormat(String path, PpFixedFormatType fixedFormatType, Object intent, Object frameSlides, Object handoutOrder, Object outputType, Object printHiddenSlides, Object printRange, Object rangeType) at PowerPointExamplesCS4.Example02.RunExample() in C:\ws\git\NetOfficeFwSample\Samples\PowerPoint\01 PowerPoint Automation Sample\PowerPointExamples\Examples\Example02.cs:line 51

Unfortunally I was unable to step into the ExportAsFixedFormat(2) method using the debugger.

jozefizso commented 3 weeks ago

You must fix your code according to the Microsoft documentation.

timsteen commented 3 weeks ago

Hi Jozef, is there any working example available?

timsteen commented 3 weeks ago

Searching the internet we found that the printRange parameter causes the error. It must be set to a value. See https://stackoverflow.com/a/74059554/11942268.

timsteen commented 3 weeks ago

The suggested solution setting the printRange parameter to

printOptions.Ranges.Add(1, Actor.Slides.Count);

caused an error in a very special case (probably because it was a pptm file)

at NetOffice.Invoker.MethodReturn(ICOMObject comObject, String name, Object[] paramsArray) at NetOffice.CoreMethodExtensions.ExecuteKnownReferenceMethodGet[T](Core value, ICOMObject caller, String name, Type knownType, Object[] paramsArray) at NetOffice.PowerPointApi.PrintRanges.Add(Int32 start, Int32 end)

But using

printOptions.Ranges.Add(1, 1);

instead works and all slides are exported to the resulting PDF.