Referencing the Application

Visual LANSA

Referencing the Application

To start an ActiveX-enabled application from a LANSA form, you need to add a reference to it. The simplest way of doing this is to drag and drop the ActiveX component to the form. This creates a DEFINE_COM statement for the component.

This is the statement created for the Word application component when the #VA_WORD component is dropped to a form:

DEFINE_COM CLASS(#VA_WORD.Application) NAME(#VA_WORD)

 

When you execute your form and this DEFINE_COM statement for Word is encountered, an instance of the Word application is started immediately. Even if you have an instance of Word running, a new instance of it is started.

Change the name of the application to #WordApp to make the example easier to follow:

DEFINE_COM CLASS(#VA_WORD.Application) NAME(#WordApp)

 

Controlling when the Reference is Created

In this example, the Word application is not to start when the LANSA form is executed. To control when Word is started, first define it with a dynamic reference and then assign a reference to it in the Click event of a button.

To make the component definition dynamic, add the Reference property to the DEFINE_COM statement like this:

DEFINE_COM CLASS(#VA_WORD.Application) NAME(#WordApp) REFERENCE(*DYNAMIC)

 

When this statement is executed, no reference to Word is created. To start Word when a user clicks on the Start Word button, assign a reference to it in the button's Click event using the SET_REF command:

EVTROUTINE HANDLING(#STARTBTN.Click)

   SET_REF COM(#WordApp) TO(*CREATE_AS #VA_WORD.application)

ENDROUTINE

 

Checking if the Reference is Exists

Trying to use any of Word's properties, methods or events before it is started would cause an error. Therefore, initially disable the controls on the form and enable them only when Word has been started. The IF_REF statement is used to check whether a reference has been created to Word. Put this statement in the Click event of the Start Word button:

EVTROUTINE HANDLING(#STARTBTN.Click)

  SET_REF COM(#WordApp) TO(*CREATE_AS #VA_WORD.application)

  

  if_ref com(#wordapp) is_not(*null)

    set com(#ShowHideBtn #gpbx_1 #rdbn_1 #rdbn_2 #rdbn_3 #Addbtn #InsBtn #SaveBtn #CloseBtn #PrintBtn) enabled(true)

  endif

ENDROUTINE

 

 

Ý 7.2.4 Example of Integrating Microsoft Word