How to implement a Copy Loop in Word report layouts - Microsoft Dynamics 365 Blog (2024)

We in the EMEA Customer Support and Services team receive from time to time a request to implement the Copy Loop feature as it has been for RDLC and/or Classic Client reports. Basically, implementing a copy loop means giving the user the capability to choose one or more copies of a specific document to be printed together with the original one in one go.

In this blog, I am proposing a way to easily implement a Copy Loop using the Word Report Layout feature that was introduced in Microsoft Dynamics NAV 2015 and enhanced version after version.

What will follow and the objects attached (reports 1305 Standard Sales – Order Conf.and 1306 Standard Sales – Invoice) are based on Microsoft Dynamics NAV 2017 CU 2 W1. Modifications made could be easily applied also to previous versions (Microsoft Dynamics NAV 2016 and Microsoft Dynamics NAV 2015).

Below, I am considering modifications to Report 1306 Standard Sales – Invoice.

C/AL application code and report changes

  1. Add the following global variables:
    Name DataTypeSubtypeLength
    NoOfCopiesInteger
    NoOfLoopsInteger
    CopyDescText30
    OldHeaderRecordSales Invoice Header
    IInteger
    RunningHeaderRecordSales Invoice Header
  2. Add the following text constant:
    NameConstValue
    CopyDescLbl*** DOCUMENT COPY ***
  3. Add the following field into Request Page
    SourceExprNameCaption
    NoOfCopiesNoOfCopiesNo. of Copies
  4. Add the following column into Header DataItem (see below lines or image)
    Data SourceName
    CopyDescCopyDesc

  5. Add an Integer DataItem called “CopyLoop” on top of all the others with indentation 0 and the following property set as per below
    DataItemTableView SORTING(Number) ORDER(Ascending)
  6. Select ALL the other elements (DataItems + Columns) except “CopyLoop” and indent them all (see picture below)
  7. Change the report property WordMergeDataItem from “Header” to “CopyLoop”.
    This is the crucial point that would instruct the application to create a brand new section and reset section page number within the Word document. Old C/AL sailors might notice that this DataItem is placed in a different position rather than where is located typically within RDLC or Classic Client reports.See the image below.
  8. Change the C/AL Code as per the following (check below code or image)

CopyLoop – OnPreDataItem()

NoOfLoops := Header.COUNT * (ABS(NoOfCopies) + 1); //MSDN-BlogCopyDesc := ''; //MSDN-BlogSETRANGE(Number,1,NoOfLoops); //MSDN-BlogFirstLineHasBeenOutput := FALSE; //MSDN-BlogOldHeader.COPY(Header); //MSDN-BlogRunningHeader.COPY(OldHeader); //MSDN-Blog

CopyLoop – OnAfterGetRecord()

IF RunningHeader.NEXT = 0 THEN BEGIN //MSDN-BlogRunningHeader.COPY(OldHeader); //MSDN-BlogRunningHeader.FINDFIRST; //MSDN-BlogCopyDesc := CopyDescLbl; //MSDN-BlogEND; //MSDN-Blog

CopyLoop – OnPostDataItem()

Header – OnPreDataItem()

SETRANGE("No.",RunningHeader."No."); //MSDN-Blog//FirstLineHasBeenOutput := FALSE;//MSDN-Blog

Word Layout creation and basic changes

With the modification provided in the C/AL application code and report changes, it is now possible to prompt users to input the number of copies and have these printed out in one go. However, there are still a couple of small modifications that needs to be done in the Word Layout. These are mainly two:

  1. Add the variable CopyDesc to the Word layout, in order to distinguish between the original and the copies.
  2. Change the page numbering (page number / total pages) in order to cope with the report refactoring.

To finalize the implementation, then, here is what to do about the Word layout:

  1. In the Windows client, search for Custom Report Layout
  2. In the Custom Report Layout window, click the Newaction button
  3. Select Report ID 1306, choose the Insert Word Layoutfield, and then press OK
  4. Rename the new created record description from Copy of Built-in Layoutto Sales – Invoice with CopyLoop
  5. Select the new record created and click the Edit Layoutaction button
  6. In the Word layout, right click in the header and select “Edit Header”
  7. Add CopyDesc as Plain Text from the Developer > XML Mapping Pane menu in the bottom of the very first rectangle and make it bold. See image below:
  8. Select the first Number right after Page_Lbl (that’s current page number), right click and select “Edit Field”. Select Field Names: Page and Format: 1,2,3, …
  9. Select the second Number right after Page_Lbl (that should be the total pages for the current document), right click and select “Edit Field”. Select Field Names: SectionPages and Format: 1,2,3, …
  10. Save, and close the Word layout, and import the changes back into NAV.
  11. Close the Custom Report Layout window
  12. Search for Report Layout Selection
  13. In the Report Layout Selection window, filter the list for Report ID 1306
  14. Change the Selected Layoutfield to Custom Layoutand choose Report ID 1306 with the description Sales – Invoice with CopyLoop. Thenclick OK to close the page and confirm.
  15. Open theReport Selections Sales window, and then choose the usage Invoice
  16. Be sure to have Report 1306 added as per the following

Everything should be now good to go.

Give it a spin with Posted Documents > Posted Sales Invoices.

These postings are provided “AS IS” with no warranties and confer no rights. You assume all risk for your use.

Duilio Tacconi (dtacconi)

Microsoft Dynamics Italy

Microsoft Customer Service and Support (CSS) EMEA

Thanks to Roberto Stefanetti from SOLUZIONIEDP.IT to open this Advisory request to EMEA CSS!

How to implement a Copy Loop in Word report layouts - Microsoft Dynamics 365 Blog (10)

Duilio Tacconi

See more articles from this author

How to implement a Copy Loop in Word report layouts - Microsoft Dynamics 365 Blog (2024)

References

Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 6267

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.