Workbook.ExportAsFixedFormat in Microsoft Dynamics AX
How to export a file to PDF in Dynamics AX
Microsoft has an extremely useful .NET method named ExportAsFixedFormat() for exporting office files to PDF. For example, you can invoke the ExportAsFixedFormat() method on an Excel Workbook object and a PDF file is then created in the folder of your choice.
Unfortunately the documentation on how to use this in function in Dynamics AX is a little lacking. In the .NET documentation most of the parameters are described as optional, while in Dynamics AX you have to pass all the parameters.
The last parameter is a pointer to the FixedFormatExt class. It is not necessary to pass an actual value, but the method does not accept null. Saveen Reddy describes in this post how to pass an empty value in C#. In Dynamics AX is even more tricky, since you do not have access to System.Reflection.Missing.Value. You need to create an object like this:
System.Type type = System.Type::GetType("System.Reflection.Missing");
System.Reflection.FieldInfo info = type.GetField("Value");
System.Object missingVariant = info.GetValue(null);
The final job is below, notice:
- All the parameters are passed, none are optional (line 32)
- The last parameter is a ‘null’ object that uses System.Reflection.Missing (lines 20-22)
- The method is in a try and is followed by a catch for a CLRError. This makes debugging much easier since it displays the .NET error which would otherwise not have been shown (lines 42-54 )
- I have used System.Type since I was worried AX variables might confuse .NET. As far as I know you could use normal AX str and int variables (lines 13-18)
static void ExcelExportPDFAsFixedFormat(Args _args)
{#File
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbooks xlWBS;
Microsoft.Office.Interop.Excel.Workbook xlWB;
System.Exception exception;Filename filename = @'C:\PDF\test3.pdf';
Filename excelFileName = @'C:\PDF\TR1-000058.xlsx';
System.Int32 toPage = 1;
System.Int32 fromPage = 1;
System.Boolean OpenAfterPublish = false;
System.Boolean IncludeDocProperties = true;
System.Boolean IgnorePrintAreas = false;
System.String fileNameDotNet = filename;
System.Type type = System.Type::GetType("System.Reflection.Missing");
System.Reflection.FieldInfo info = type.GetField("Value");
System.Object missingVariant = info.GetValue(null);
new InteropPermission(InteropKind::ClrInterop).assert();
xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlWBs = xlApp.get_Workbooks();
xlWB = xlWBS.Add(excelFileName);
try{xlWB.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType::xlTypePDF,
fileNameDotNet,Microsoft.Office.Interop.Excel.XlFixedFormatQuality::xlQualityStandard,
IncludeDocProperties,IgnorePrintAreas,fromPage,toPage,OpenAfterPublish,missingVariant);}catch(Exception::CLRError)
{exception = ClrInterOp::getLastException();
while (exception)
{error(exception.get_Message());
exception = exception.get_InnerException();
}}catch
{error("An unknown exception has occurred");
}CodeAccessPermission::revertAssert();
}
It is also possible to create PDF’s form Word Documents. I’ll post an example if I write a job to do that in the future.
UPDATE 26 December 2014:
A senior colleague wanted to achieve something similar and I offered him this job. He pointed out that an instance of Excel is opened every time the job is run, but never closed. You can see this clearly in the Task Manager.
If you want to use this job, experiment with adding xlWB.Close and/or xlApp.Quit, and keep an eye op the running applications in the Task Manager.

Like
Report
*This post is locked for comments