How to store and retrieve an Office document from/to an AX table.

Have you searched the net for how to store documents in an AX table? I did, a lot.. When I couldn’t find it, I had to do it myself 😉 Here is a short description on how to store, download and edit documents from AX in Office (or any..) program.


First you need to create a table with a field of the type Container. Then add the Extended data type BinData to that field. My example table looks like this ;

1
2
3
4
5
FO_DocuTest:
   int          FileNum 
   str          FileName
   str          FileType // doc, xls or whatever
   container    FileData // BinData as extended.

First, here is a short table method on how to load from disk.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// load from disk
void load(str _path)
{
    BinData binData = new BinData();
    ;
 
    if (binData.loadFile(_path + "\\" + 
                         this.FileName + "." + 
                         this.FileType)) 
    {
        //only works if file not locked. 
        this.FileData = binData.getData();
    }
    else
        throw error(strfmt("@SYS54217",_path));
}

Second, a short table method on how to save back to disk.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// save to disk
void save(str _path)
{
    BinData binData = new BinData();
    ;
 
    binData.setData( this.FileData );
    if (!binData.saveFile(_path + "\\" + 
                          this.FileName + "." + 
                          this.FileType)) 
    {
        //only works if file not locked
        throw error(strfmt("@SYS54217",_path));
    }
}

Then you have these two, you only need to know how to start Word/Excel. I use Word in this example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void clicked() // click method in a form.
{
    COM     app,documents,doc;
    ;
 
    super();
    FO_DocuTest.save(mTempDirectory); // save blob to disk.
 
    app = new com("Word.Application");
    documents = app.Documents();
    doc = documents.open(mTempDirectory + "\\" +
                         FO_DocuTest.FileName + "." +
                         FO_DocuTest.FileType);
    doc.activate();
    app.visible(true);
}

And finally, a method on how to upload back to table again.

1
2
3
4
5
6
7
8
9
10
11
void clicked()  // click method in a form.
{
    super();
    FO_DocuTest.load(mTempDirectory); // load back to blob
    FO_DocuTest.update();
 
    // remove temp file again.
    WinApi::deleteFile(mTempDirectory + "\\" +
                       FO_DocuTest.FileName + "." +
                       FO_DocuTest.FileType); 
}

Voila! Not more than that… After searching around on the net, I found it strange that no one presented this, but now you have it!

3 thoughts on “How to store and retrieve an Office document from/to an AX table.

  1. Thanks. Yes, It’s there I got some of the ideas from, but I wanted an isolated solution to demonstrate. DocuRef, DocuTable etc contains so much more and would make a much bigger article. I wanted to pick out only the pure basics needed to explain this.

    /Henrik

Leave a Reply

Your email address will not be published. Required fields are marked *