The Beloved Report
"Oh Excel, how I love thee, let me count the ways! A1, A2, A3, A4…"
Of all the development tasks I’m asked to do the most common has got to be to add Excel output to a NAV report. Most often, the request is specifically for the Detail Trial Balance or Inventory Valuation report.
Years ago when I first started working with NAV I decided the Excel Buffer Table method was just not my cup of tea. Now you may use it and find that it works just fine to accommodate all your needs. I, however, like to flavor my spreadsheets with color and multiple fonts and even turning some values red when they should draw attention.
I’ve also found that the temporary table size restriction of 2GB can prevent some reports from being created using the Excel Buffer Table. Likewise, the SAVEASEXCEL function has a 64K limit, so you can’t use it for larger reports either. And, if you’re running on the RTC client, know that the automation is invoked on each row, which tends to slow down performance on larger reports as well.
But it was solely from the desire to make the Excel reports “fancier” that I developed a codeunit full of Excel commands. You will recall from a blog article a few weeks ago that we talked about reusing code, and placing all our reusable code into a single codeunit, which makes all the functions you need easy to find and readily accessible. This is a prime example of that, and the most used of those codeunits that I have. It’s probably been placed in somewhere around 50 different databases so far. Today, it’s yours for free! Enjoy! And I’m going to show you how to use it.
But there’s no need to fill up the tank if you don’t have the keys! In order to use the codeunit you will have to have Microsoft Excel loaded – and not just the reader, but the full version. You can use Excel from the 2010 version back to the 1995 version, and possibly older version. The codeunit uses Excel as an Automation, which is a data type in NAV that is used to incorporate external software into NAV functions using COM technology. In the classic client environment, Excel would reside on the client machine. But when using the Role Tailored Client, whether Excel resides on your client or your NAV server is dependent on several factors. Click here to read about those.
HOW TO FIND ADDITIONAL COMMANDS FOR EXCEL
You will find the functions within the codeunit do a number of different things using Excel classes, but you may decide you want to add a few more. (If you do – please share them back using the comment box below). The easiest way to find the commands to use is by recording a macro in Excel, and reviewing the VBA command, which is not exactly like NAV uses it, but very close.
In Excel 2010, the commands to create a macro are: View-Macro-Record Macro-(name it)-and click OK. To stop the recording, just click Macro-Stop Recording. To see the commands used by Excel, use Macro-View Macro-Edit. In my simple test for bolding the contents of a single cell, I turned on the macro record function, entered “ABC” into a cell, and then bolded the cell. I then stopped the macro recording, and edited the macro. The results look something like this:
Sub Macro1() Macro1 Macro ActiveCell.FormulaR1C1 = "ABC" Range("A1").Select Selection.Font.Bold = True End Sub
But in NAV, using the Excel automation, the command to bold a single cell looks like this:
xlSheet[Sheet1].Range(A1).Font.Bold := 1;
Okay, stay with me now! It’s really not all that complicated. We first have to identify the sheet in which we are bolding the cell. (In the macro you don’t see this because the macro is already on the sheet. It doesn’t record what you’ve already done prior to setting it to record.) Then we have to identify the Range, which is ‘A1’. Then we just execute the “Font.Bold” class of Excel by assigning it a value of 1.
If you don’t “get it” right from the beginning, don’t sweat it. Nearly any example you need is found in the codeunit already. Just dig through it and you’ll either find what you’re looking for - or find something close enough to lead you to what you need.
If you’re ambitious and you want to look at the various classes and parameters for Excel, while editing the macro click F2 (Object Browser), and set your library to EXCEL. You’ll see all the classes of Excel, and be able to drill down to the different settings for each one. When you see what you’re looking for, right click on the class, and select HELP, which almost always gives you the full command.
Below you’ll see what this looks like when I search for the syntax for setting the font color for a range:
USING THE CODEUNIT
The beauty of the codeunit is that each function can simply be passed the values it needs (such as sheet number, range, font size, color name, etc…) and it will just handle the commands for you. So if you learn how to call the commands using these variables, you really don’t have to learn the excel commands at all! About the only way you can mess it up is by doing things out of order. For example, you can’t bold a field before you create the sheet.
Let’s go through a quick example, and I’m going to show you just how easy it is to add Excel output to the Inventory Valuation report. (It’s really just cut, paste, modify a bit, and go!) I’ll also give you some tips on designing Excel output from NAV. And I will give you an iron clad, money-back guarantee that you can do this faster with the codeunit than with the Excel Buffer table! (It’s free, remember?)
I will be using a standard, NAV 2009 R2 version, of the Inventory Valuation report 10139. If you’re not yet on NAV 2009 R2, don’t worry! The report layout has changed very little through past versions. The code inserted should be the same for versions even 10 years old, as long as you haven’t customized your report.
First, download the codeunit below in text format.
(Right click and “SAVE” to download)
Since it’s in text format, you can open it and change the object number to something good for your database and license prior to importing it. Then simply import it to your development database (remember – we never do development in our production database), and compile it. As long as you have Excel loaded, the compile shouldn’t give you problems at all.
Now, let’s get started adding Excel output to our Inventory Valuation report! Watch the video below and I’ll take you through it step by step and show you how it’s done. Then try it! You’ll be surprised just how easy it is to add an Excel report to any NAV report.
But don’t stop there! Grow the codeunit by adding new functions! You might want to add a sort function where you pass it a few columns for sort priority, or maybe a pivot table function. If you add the functions to it to perform the things your users tend to do most, and then do it for them in NAV, imagine how much your users will love you! Why, you’ll feel as if you’re wearing Super Man’s cape!