Skip to main content

Integration with word

In this section i am going to write about word integration with AX.
Let us first start with a small example to show how it all really works. Below is the piece of code which opens up a specified document and gets the data written in it.
static void getWordData(Args _args)
{
COM document;
COM wordDocument;
COM range;
COM app;
;
app = new com("Word.Application");
document = app.Documents();
wordDocument = document.add(‘E:\\Docs\\Extend the Alerts Functionality.doc’);
range = wordDocument.range();
info(range.text());
app.quit(0);
}
Here we use Word.Application COM class for integration with word. We open the document "Extend the Alerts Functionality.doc’ and display its contents in the infolog.
Now let us go ahead with opening a word document and writing data in it through AX. Below is a small piece of code that will open a word document and write "Example of word integration with AX.".
static void writeWordData(Args _args)
{
COM document;
COM wordDocument;
COM range;
COM app;
;
app = new COM(‘Word.Application’);
app.visible(true);
document = app.Documents();
wordDocument = document.add();
wordDocument.activate();
range = wordDocument.range();
range.insertBefore(‘Example of word integration with AX.’);
}
This is how the document looks
Formatting the text
Now let us go ahead and look how we can format the text we have written in the example above. We will increase its size, add a background color and make it bold.
Below is the revised code
static void writeWordData(Args _args)
{
COM document;
COM wordDocument;
COM range;
COM app;
COM font;
COM shading;
;
app = new COM(‘Word.Application’);
app.visible(true);
document = app.Documents();
wordDocument = document.add();
wordDocument.activate();
range = wordDocument.range();
range.insertBefore(‘Example of word integration with AX.’);

font = range.font();
font.Size(16);
font.bold(2);

shading = font.shading();

//You can use either the integer directly or better way is to use RGB2Int
//shading.BackgroundPatternColor(65535); //Yellow
shading.BackgroundPatternColor(WinApi::RGB2int(255, 255, 0)); //Yellow
}
When code is run following result is achieved.

Aug
In this section let us learn to do following things in integration with word
    • Creating tables
    • Applying Styles to tables
    • Adding rows to tables
    • Getting cells and filling data



Creating Tables
To add a table first we need to get the current range (it can be either start of the document or last cell of the document). Let us assume that we have start of the document. So your code should look something like this
Range = Document.Range(0, 0);
Where ‘Range’ and ‘Document’ are variables of type COM. Now After getting the range of the document to insert the table, we need to get the reference to tables from the document so you can write the code as follows
Tables = Document.Tables();
Now add a table at the specified range. To do this you need to make a call to method add() of ‘Tables’ object. The method add takes three arguments 1. Range – position to insert 2. Rows – Number of rows, 3. Columns – Number of columns. Below is the line of code to be written
Table = Tables.Add(range , 1, 3);
Applying Styles to Tables
To apply a particular style to table, you first need to know the name of the style. This you can find out from Styles manager in word. Then you just need to write following code to apply the style
Table.style(‘Table Grid’);
Adding Rows to Table
To add a row to table we first need to get all the rows from the table and then call add method. Just write following code
rows = table.rows();
rows.add();
Getting cells and filling data
Write following code to get a cell and fill data.
cell = table.cell(1, 1);
range = cell.range();
range.text(‘Test’);
Example
Following example code illustrate following
    • Open a word document
    • Add a table
    • Write to cells
    • Add rows to table
    • Apply styles to table
    • Format the first row of the table





static void writeTableInWord(Args _args)
{
COM app, document, wordDocument;
COM range, tables, table;
COM cell, cells, rows, row, shading, font, paragraphs;
int i, j;
SysDictTable sysDictTable = new SysDictTable(tableNum(CustGroup));
SysDictField sysDictField;
;
app = new COM(‘Word.Application’);
app.visible(true);

document = app.Documents();
wordDocument = document.add();
wordDocument.activate();

range = wordDocument.range(0,0);
tables = wordDocument.tables();

//Add the table
table = tables.add(range , 1, 3);
table.style(‘Table Grid’);

//Write to cells
cell = table.cell(1,1);
range = cell.range();
range.text(‘Field Name’);
cell = table.cell(1,2);
range = cell.range();
range.text(‘Type’);
cell = table.cell(1,3);
range = cell.range();
range.text(‘Description’);
for (i = 1; i <= sysDictTable.fieldCnt(); i++)
{
sysDictField = sysDictTable.fieldObject(i);

if (sysDictField && !sysDictField.isSystem())
{
//Add a row to the table
rows = table.rows();
rows.add();

cell = table.cell(i + 1, 1);
range = cell.range();
range.text(sysDictField.name());

cell = table.cell(i + 1, 2);
range = cell.range();
range.text(enum2str(sysDictField.baseType()));

cell = table.cell(i + 1, 3);
range = cell.range();
range.text(sysDictField.help());
}
}

//Formatting first row of the table
//Get the first row
row = rows.first();
cells = row.cells();
cells.verticalAlignment(1); //Middle
row.setHeight(20, 2);
shading = row.shading();
shading.backgroundPatternColor(10526880); //Grey 375
range = row.range();
font = range.font();
font.bold(1); //Bold
paragraphs = range.paragraphs();
paragraphs.alignment(1); //Center
}
After running the job following output can be seen in the word document




Field Name
Type
Description
CustGroup
String
Group of customers.
Name
String
Describe the customer group
ClearingPeriod
String
Terms of payment for period from due date to payment.
PaymTermId
String
Terms of payment used with forecast coverage plan for the group
   

Popular posts from this blog

What does this mean: "The form datasource query object does not support changing its AllowCrossCompany property after the form has executed the query."?

I have made a form with datasources vendtable and vendtrans. Inside vendtable_ds.executequery() looks like this: QueryBuildDataSource queryBuildDatasource ,queryBDS_VendTrans_Invoice; ; queryBuildDatasource = this.query().dataSourceTable(tablenum(vendtable)); queryBDS_VendTrans_Invoice = this.query().dataSourceTable(tablenum(vendtrans)); if (curext() == "MASTERCOMP") { this.query().allowCrossCompany(true); } else { this.query().allowCrossCompany(false); } //FilterVendorName = stringedit control on form if (FilterVendorName.text()) { queryBuildDatasource.addRange(fieldNum(VendTable,Name)).value(strfmt("*%1*", FilterVendorName.text())); } else { queryBuildDatasource.clearRange(fieldNum(VendTable,Name)); } //FilterInvoiceNumber = stringedit control on form if (FilterInvoiceNumber.valueStr() == "") { queryBDS_VendTrans_Invoice.enabled(false); } else { queryBDS_VendTrans_Invoice.enabled(true); queryBDS_VendTrans_In...

Credit Note [Dynamics AX] using X++

This post will help to create credit note for a sales order based on the invent lot id. All the invoices raised for a particular sales line – Lot Id will be raised back as a credit note. Information on Credit Note: A credit note or credit memorandum (memo) is a commercial document issued by a seller to a buyer. The seller usually issues a Credit Memo for the same or lower amount than the invoice, and then repays the money to the buyer or sets it off against a balance due from other transactions Below Code will help to create credit note for all the invoices raised against the sales line -lot id. Please note: This code can be customized as per your requirements. This is just a template to help creating credit note using X++ code. Please test the code before use. static void SR_CreateCreditNote_Sales(Args _args) { // Coded by Sreenath Reddy CustInvoiceTrans custInvoiceTrans; Dialog dialog = new Dialog(“Create credit note – for sales.”); DialogField dfInv...