From X++

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:

  1.     System.Type                  type           = System.Type::GetType("System.Reflection.Missing");
  2.     System.Reflection.FieldInfo  info           = type.GetField("Value");
  3.     System.Object                missingVariant = info.GetValue(null);

The final job is below, notice:

  1. All the parameters are passed, none are optional (line 32)
  2. The last parameter is a ‘null’ object that uses System.Reflection.Missing (lines 20-22)
  3. 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 )
  4. 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)

  1. static void ExcelExportPDFAsFixedFormat(Args _args)
  2. {
  3.     #File
  4.     Microsoft.Office.Interop.Excel.Application  xlApp;
  5.     Microsoft.Office.Interop.Excel.Workbooks    xlWBS;
  6.     Microsoft.Office.Interop.Excel.Workbook     xlWB;
  8.     System.Exception                            exception;
  10.     Filename                                    filename =  @'C:\PDF\test3.pdf';
  11.     Filename                                    excelFileName = @'C:\PDF\TR1-000058.xlsx';
  13.     System.Int32                                toPage                  = 1;
  14.     System.Int32                                fromPage                = 1;
  15.     System.Boolean                              OpenAfterPublish        = false;
  16.     System.Boolean                              IncludeDocProperties    = true;
  17.     System.Boolean                              IgnorePrintAreas        = false;
  18.     System.String                               fileNameDotNet          = filename;
  20.     System.Type                                 type = System.Type::GetType("System.Reflection.Missing");
  21.     System.Reflection.FieldInfo                 info = type.GetField("Value");
  22.     System.Object                               missingVariant = info.GetValue(null);
  24.     new InteropPermission(InteropKind::ClrInterop).assert();
  26.     xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
  27.     xlWBs  = xlApp.get_Workbooks();
  28.     xlWB   = xlWBS.Add(excelFileName);
  30.     try
  31.     {
  32.         xlWB.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType::xlTypePDF,
  33.                                  fileNameDotNet,
  34.                                  Microsoft.Office.Interop.Excel.XlFixedFormatQuality::xlQualityStandard,
  35.                                  IncludeDocProperties,
  36.                                  IgnorePrintAreas,
  37.                                  fromPage,
  38.                                  toPage,
  39.                                  OpenAfterPublish,
  40.                                  missingVariant);
  41.     }
  42.     catch(Exception::CLRError)
  43.     {
  44.         exception = ClrInterOp::getLastException();
  45.         while (exception)
  46.         {
  47.             error(exception.get_Message());
  48.             exception = exception.get_InnerException();
  49.         }
  50.     }
  51.     catch
  52.     {
  53.         error("An unknown exception has occurred");
  54.     }
  55.     CodeAccessPermission::revertAssert();
  56. }

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.

How to generate a list of field names and field properties

Today I quickly wanted to see which fields in the PurchLine table are mandatory. Purchline is a large table, so instead of checking the properties of each field, I quickly wrote a job that displays an info log of all the mandatory fields. I used the DictClass example script from Microsoft, and adapted it so that it generates a list with all the fields in the table.

This job can be easily changed to display the fields of other tables, simply change the tableName variable. It can also be changed to display other properties like ‘Allows edit on create’. For this you need to change the #DBF_Mandatory flag to another flag defined in the macro:
DictField Macro

This script uses a number of interesting things like macros, dictField and dictTable. I’ll write separate posts for these topics soon. Until then, here is a simple jobs that lists all the fields in a table and their properties:

  1. static void getMandatoryFields(Args _args)
  2. {
  3.    #macrolib.dictfield  
  4.     DictField   dictField;
  5.     DictTable   dictTable;    
  6.     FieldId     fieldId;
  7.     int         nFlags;
  8.     TableName   tableName = "PurchLine";
  9.     ;
  10.     dictTable   = new dictTable(tableName2Id(tableName));
  11.     fieldId     = dictTable.fieldNext(0);    
  12.     while (fieldId)
  13.     {
  14.         dictField = new DictField(tableName2Id(tableName), fieldId);
  15.         if (dictField)
  16.         {
  17.             nFlags = dictField.flags();      
  18.             if (bitTest(nFlags, #DBF_MANDATORY))
  19.             {
  20.                 info(strFmt("The field %1 is mandatory.",;
  21.             }
  22.             else
  23.             {
  24.                 //info(strFmt("The field %1 is not mandatory.",;
  25.             }
  26.         }        
  27.         fieldId = dictTable.fieldNext(fieldId);
  28.     }
  29. }

The resulting info log should look something like this:
Mandatory purchline fields