Using the Add Method


To create a new document, use the Add method of the documents collection of the Word application. In this method you can specify the Word template on which the new document is based on. The method returns a reference to the newly created document. You assign this return value to a component name using the Add_RETVAL parameter.

In order to use the Add_RETVAL parameter to assign a component reference to the new document, the component has to first be defined. This component must be of the type Word document and it must be defined with a dynamic reference:


In this example, the Add method in the Click event of the New Document button is invoked. The new document will be based on the template which is installed as part of the LANSA demonstration system. Give the new document the component name #Worddoc:


invoke method(#WordApp.documents.add) template('C:\X_Win95\x_lansa\x_dem\source\') add_retval(#WordDoc)

set #std_num value(#WordApp.Documents.count)



Assigning a component name to the new document can be useful. However, there are also other ways of identifying a document. You can, for example, refer to the currently active document (#WordApp.ActiveDocument) or you can identify it by its name (#WordApp.Documents<'Report.doc'>) or number (#WordApp.Documents<2>) in the documents collection.