Creating a Word document with repeating in Dynamics ax 2012

In this recipe, we will create a Word document with repeating elements. For this demonstration, we will display the contents of the LedgerParameters table in a dynamically generated Word table
we need to prepare a new Word template and save it as a file named table.dotx. The template should contain one bookmark named TableName at the top, and one table beneath with a single row and two columns, as follows:
w2

In the AOT, create a new job named CreateWordTable with the following code:
static void CreateWordTable(Args _args)
{
TableId tableId;
COM word;
COM documents;
COM document;
COM bookmarks;
COM bookmark;
COM tables;
COM table;
COM rows;
COM row;
COM cells;
COM cell;
COM range;
Query query;
QueryRun queryRun;
Common common;
TmpSysTableField fields;
DictField dictField;
int i;
void processBookmark(str _name, str _value)
{
if (!bookmarks.exists(_name))
{
return;
}
bookmark = bookmarks.item(_name);
range = bookmark.range();
range.insertAfter(_value);
}
#define.Word(‘Word.Application’)
#define.template(@’C:\temp\table.dotx’);
tableId = tableNum(LedgerParameters);
try
{
word = new COM(#Word);
}
catch (Exception::Internal)
{
if (word == null)
{
throw error(“Microsoft Word is not installed”);
}
}
documents = word.documents();
document = documents.add(#template);
bookmarks = document.bookmarks();
processBookmark(
‘TableName’,
tableId2pname(tableId));
tables = document.tables();
table = tables.Item(1);
rows = table.rows();
query = new Query();
query.addDataSource(tableId);
queryRun = new QueryRun(query);
queryRun.next();
common = queryRun.get(tableId);
fields = TmpSysTableField::findTableFields(
null, tableId);
while select fields
{
dictField = new DictField(
tableId,
fields.FieldId);
if (dictField.isSystem())
{
continue;
}
i++;
row = rows.item(i);
cells = row.cells();
cell = cells.item(1);
range = cell.range();
range.insertAfter(fields.FieldLabel);
cell = cells.item(2);
range = cell.range();
range.insertAfter(
strFmt(‘%1’, common.(fields.FieldId)));
row = rows.add();
}
row.delete();
word.visible(true);
}

2. Run the job to generate the document containing a list, the LedgerParameters table
field, and their values:

w23

Advertisements

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

%d bloggers like this: