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.
 
 
 
 

One thought on “Integration with word

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s