Multiple Purchase Lines in Word Template for Automation

snehalppatelsnehalppatel Member Posts: 34
I am trying to create Word Automation functionality to generate .doc file for Purchase Quote. I am able to get all other fields. But I am bit confused about how to implement the Purchase Lines in the word template. Basically the structure of the Purchase Quote in my word document will be somewhat similar to the one shown below:

Description - Qty - Rate - Amount
Item1 - 1 - 10 - 10
Item2 - 5 - 5 - 25


Now, my question is can i keep only one variable for all the fields and loop it through to export multiple lines in the word file? If not, what is the ideal solution to export multiple lines to word from NAV? I would be grateful, if some code is also posted to guide.

Thanks in Advance.
Snehal Kumar Patel
MCTS - Microsoft Dynamics NAV

Answers

  • snehalppatelsnehalppatel Member Posts: 34
    Hello Friends,

    I am able to solve my problem by searching some code to play with objects of Word Template using VBA. I found that I can get template's Table object at run-time and add new Rows in it using automation C/AL code. I am herewith posting the code for others to refer to that solved my problem.

    In the code I have used variables as shown below:
    wdTable - Automation - 'Microsoft Word 11.0 Object Library'.Table
    Counter - Integer
    PurchaseLine - Record - Purchase Line

    What I wanted to achieve was filling values in the rows of table from Purchase Line table. I have already a table in my Word template with headings "Description", "Qty","UOM","Cost","Amount". Hence, I initialized the variable Counter with value "1". Added a new row to the existing table. I looped through Purchase Lines for a particular Purchase Quote and assigned values of each field to respective cells of the row.
      wdTable := wdApp.ActiveDocument.Tables.Item(1);
    
      Counter := 1;
    
      IF PurchaseLine.FIND('-') THEN
        REPEAT
          wdRow := wdApp.ActiveDocument.Tables.Item(1).Rows.Add;
          Counter := Counter + 1;
    
          wdTable.Cell(Counter,1).Range.Text := PurchaseLine.Description;
          wdTable.Cell(Counter,2).Range.Text := FORMAT(PurchaseLine.Quantity);
          wdTable.Cell(Counter,3).Range.Text := PurchaseLine."Unit of Measure Code";
          wdTable.Cell(Counter,4).Range.Text := FORMAT(PurchaseLine."Direct Unit Cost");
          wdTable.Cell(Counter,5).Range.Text := FORMAT(PurchaseLine."Line Amount");
        UNTIL PurchaseLine.NEXT = 0;
    

    Voilla...I am happy :D
    Snehal Kumar Patel
    MCTS - Microsoft Dynamics NAV
Sign In or Register to comment.