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

Dynamics Axapta: Sales Orders & Business Connector

Well, again folllowing my same idea of writting close to nothing and pasting code, I'll paste in some code to create a sales order from some basic data and the invoice it. I'll try to explain more in the future. AxaptaObject axSalesTable = ax.CreateAxaptaObject("AxSalesTable"); AxaptaRecord rcInventDim = ax.CreateAxaptaRecord("InventDim"); AxaptaRecord rcCustTable = ax.CreateAxaptaRecord("CustTable"); rcCustTable.ExecuteStmt("select * from %1 where %1.AccountNum == '" + MySalesOrderObject.CustAccount + "'"); if (MySalesOrderObject.CurrencyCode.Trim().Length == 0) MySalesOrderObject.CurrencyCode = rcCustTable.get_Field("Currency").ToString().Trim(); string sTaxGroup = rcCustTable.get_Field("taxgroup").ToString().Trim(); //set header level fields axSalesTable.Call("parmSalesName", MySalesOrderObject.SalesName.Trim()); axSalesTable.Call("parmCustAccount", M

Passing values between form and class

Class name is EmplDuplication and Form is EmplTable . void clicked() {    MenuFunction mf;    args args = new Args();    ;     args.record(EmplTable);     mf = new menufunction(identifierstr(EmplDuplication), MenuItemType::Action); mf.run(args); } Meanwhile, in the main() method of the EmplDuplication class, we need to put this Axapta x++ code to get the datasource: static void main(Args args) {     EmplDuplication EmplDuplication; EmplTable localEmplTable; ;     if(args.record().TableId == tablenum(EmplTable)) localEmplTable = args.record();     ... }