Example Source

Here is the source code for the Word integration example:

***************************************************;

* ;

* COMPONENT: STD_FORM ;

* ;

***************************************************;

FUNCTION OPTIONS(*DIRECT)

BEGIN_COM ROLE(*EXTENDS #PRIM_FORM) CAPTION('Microsoft Word ActiveX Example') FORMPOSITION(ScreenCenter) HEIGHT(475) LEFT(308) TOP(118) WIDTH(562)

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

DEFINE_COM CLASS(#VA_WORD.Document) NAME(#WORDDOC) REFERENCE(*DYNAMIC)

 

DEFINE_COM CLASS(#PRIM_PHBN) NAME(#STARTBTN) CAPTION('Start Word') DISPLAYPOSITION(2) LEFT(24) PARENT(#GPBX_2) TABPOSITION(2) TOP(32) WIDTH(105)

DEFINE_COM CLASS(#PRIM_PHBN) NAME(#ADDBTN) CAPTION('Add Document') DISPLAYPOSITION(1) ENABLED(False) LEFT(16) PARENT(#GPBX_2) TABPOSITION(1) TOP(224) WIDTH(113)

DEFINE_COM CLASS(#PRIM_LTVW) NAME(#LTVW_1) DISPLAYPOSITION(1) FULLROWSELECT(True) HEIGHT(329) LEFT(16) PARENT(#GPBX_3) TABPOSITION(1) TOP(64) WIDTH(161)

DEFINE_COM CLASS(#PRIM_LVCL) NAME(#LVCL_1) DISPLAYPOSITION(1) PARENT(#LTVW_1) SOURCE(#GIVENAME) WIDTH(38)

DEFINE_COM CLASS(#PRIM_LVCL) NAME(#LVCL_2) DISPLAYPOSITION(2) PARENT(#LTVW_1) SOURCE(#SURNAME) WIDTH(77) WIDTHTYPE(Remainder)

DEFINE_COM CLASS(#PRIM_PHBN) NAME(#PRINTBTN) CAPTION('Print') DISPLAYPOSITION(1) ENABLED(False) LEFT(24) PARENT(#GPBX_4) TABPOSITION(1) TOP(216) WIDTH(104)

DEFINE_COM CLASS(#PRIM_LVCL) NAME(#LVCL_3) PARENT(#LTVW_1) SOURCE(#ADDRESS1) VISIBLE(False) WIDTH(20)

DEFINE_COM CLASS(#PRIM_LVCL) NAME(#LVCL_4) PARENT(#LTVW_1) SOURCE(#ADDRESS2) VISIBLE(False) WIDTH(20)

DEFINE_COM CLASS(#PRIM_LVCL) NAME(#LVCL_5) PARENT(#LTVW_1) SOURCE(#ADDRESS3) VISIBLE(False) WIDTH(20)

DEFINE_COM CLASS(#PRIM_LVCL) NAME(#LVCL_6) PARENT(#LTVW_1) SOURCE(#POSTCODE) VISIBLE(False) WIDTH(20)

DEFINE_COM CLASS(#PRIM_LVCL) NAME(#LVCL_7) PARENT(#LTVW_1) SOURCE(#PHONEHME) VISIBLE(False) WIDTH(20)

DEFINE_COM CLASS(#PRIM_LVCL) NAME(#LVCL_8) PARENT(#LTVW_1) SOURCE(#PHONEBUS) VISIBLE(False) WIDTH(20)

DEFINE_COM CLASS(#PRIM_PHBN) NAME(#SAVEBTN) CAPTION('Save ...') DISPLAYPOSITION(3) ENABLED(False) LEFT(24) PARENT(#GPBX_4) TABPOSITION(3) TOP(184) WIDTH(104)

DEFINE_COM CLASS(#PRIM_GPBX) NAME(#GPBX_1) CAPTION('Word WindowState') DISPLAYPOSITION(4) ENABLED(False) HEIGHT(81) LEFT(16) PARENT(#GPBX_2) TABPOSITION(4) TABSTOP(False) TOP(120) WIDTH(129)

DEFINE_COM CLASS(#PRIM_RDBN) NAME(#RDBN_1) CAPTION('Minimized') DISPLAYPOSITION(1) ENABLED(False) LEFT(15) PARENT(#GPBX_1) TABPOSITION(1) TOP(14) WIDTH(82)

DEFINE_COM CLASS(#PRIM_RDBN) NAME(#RDBN_2) BUTTONCHECKED(True) CAPTION('Normal') DISPLAYPOSITION(2) ENABLED(False) LEFT(16) PARENT(#GPBX_1) TABPOSITION(2) TOP(32) WIDTH(89)

DEFINE_COM CLASS(#PRIM_RDBN) NAME(#RDBN_3) CAPTION('Maximized') DISPLAYPOSITION(3) ENABLED(False) LEFT(16) PARENT(#GPBX_1) TABPOSITION(3) TOP(50) WIDTH(81)

DEFINE_COM CLASS(#PRIM_GPBX) NAME(#GPBX_2) CAPTION('Word Application') DISPLAYPOSITION(1) HEIGHT(409) LEFT(0) PARENT(#COM_OWNER) TABPOSITION(1) TABSTOP(False) TOP(8) WIDTH(161)

DEFINE_COM CLASS(#PRIM_GPBX) NAME(#GPBX_3) CAPTION('Data for New Document') DISPLAYPOSITION(2) ENABLED(False) HEIGHT(409) LEFT(168) PARENT(#COM_OWNER) TABPOSITION(2) TABSTOP(False) TOP(8) WIDTH(201)

DEFINE_COM CLASS(#PRIM_GPBX) NAME(#GPBX_4) CAPTION('Work with Document') DISPLAYPOSITION(3) ENABLED(False) HEIGHT(401) LEFT(376) PARENT(#COM_OWNER) TABPOSITION(3) TABSTOP(False) TOP(16) WIDTH(169)

DEFINE_COM CLASS(#PRIM_PHBN) NAME(#CLOSEBTN) CAPTION('Close') DISPLAYPOSITION(2) ENABLED(False) LEFT(24) PARENT(#GPBX_4) TABPOSITION(2) TOP(248) WIDTH(105)

DEFINE_COM CLASS(#PRIM_LTVW) NAME(#LTVW_2) COLUMNHEADERS(False) DISPLAYPOSITION(4) HEIGHT(145) LEFT(16) PARENT(#GPBX_4) TABPOSITION(4) TOP(24) WIDTH(137)

DEFINE_COM CLASS(#PRIM_LVCL) NAME(#LVCL_9) DISPLAYPOSITION(1) PARENT(#LTVW_2) SOURCE(#STD_TEXT) WIDTH(100)

DEFINE_COM CLASS(#STD_NUM) NAME(#I)

DEFINE_COM CLASS(#PRIM_PHBN) NAME(#SHOWHIDEBTN) CAPTION('Show/Hide Word') DISPLAYPOSITION(3) ENABLED(False) LEFT(24) PARENT(#GPBX_2) TABPOSITION(3) TOP(72) WIDTH(101)

DEFINE_COM CLASS(#PRIM_LABL) NAME(#LABL_1) CAPTION('The document will be based on the Word template vl_sam099.dot installed by Partition Initialization.') DISPLAYPOSITION(5) ENABLED(False) HEIGHT(81) LEFT(16) PARENT(#GPBX_2) TABPOSITION(5) TABSTOP(False) TOP(256) WIDTH(121)

DEFINE_COM CLASS(#PRIM_STBR) NAME(#STBR_1) DISPLAYPOSITION(4) HEIGHT(25) LEFT(0) MESSAGEPOSITION(1) PARENT(#COM_OWNER) TABPOSITION(4) TOP(423) WIDTH(554)

DEFINE_COM CLASS(#PRIM_LABL) NAME(#LABL_2) CAPTION('Double-click an employee to insert data.') DISPLAYPOSITION(2) ENABLED(False) HEIGHT(29) LEFT(16) PARENT(#GPBX_3) TABPOSITION(2) TABSTOP(False) TOP(32) WIDTH(153)

DEFINE_COM CLASS(#PRIM_PHBN) NAME(#EXITBTN) CAPTION('Exit Word') DISPLAYPOSITION(6) ENABLED(False) LEFT(24) PARENT(#GPBX_2) TABPOSITION(6) TOP(360) WIDTH(97)

 

Def_list name(#skills) fields(#skilcode #SkilDesc #dateacqr #grade #comment #dateacq #gradedes) type(*working) Entrys(9999)

 

EVTROUTINE HANDLING(#STARTBTN.Click)

*Start Word by setting a reference to it. Word has been enrolled as ActiveX component VA_WORD

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

*

*If Word has been started, enable some controls and issue a message

if_ref com(#wordapp) is_not(*null)

set com(#showhidebtn #gpbx_1 #rdbn_1 #rdbn_2 #rdbn_3 #Addbtn #labl_1 #exitbtn ) enabled(true)

execute infmessage 'Word has been started.'

endif

ENDROUTINE

 

EVTROUTINE HANDLING(#showhidebtn.Click)

*If Word is hidden make it visible and vice versa

if cond('#wordapp.visible *eq True')

SET #WordApp Visible(false)

else

SET #WordApp Visible(true)

endif

ENDROUTINE

 

EVTROUTINE HANDLING(#RDBN_1.Click)

*Set the size of the Word window using the Windowstate property of the Word application object.

IF COND('#rdbn_1.buttonchecked *eq true')

SET #WordApp Windowstate(VA_WORD.wdWindowStateMinimize)

endif

ENDROUTINE

EVTROUTINE HANDLING(#RDBN_2.Click)

if cond('#rDbn_2.buttonchecked *eq True')

SET #WordApp Windowstate(VA_WORD.wdWindowStateNormal)

endif

ENDROUTINE

EVTROUTINE HANDLING(#RDBN_3.Click)

if cond('#rdbn_3.buttonchecked *eq True')

SET #WordApp Windowstate(VA_WORD.wdWindowStateMaximize)

endif

ENDROUTINE

 

EVTROUTINE HANDLING(#EXITBTN.Click)

* Close Word without saving any changes

invoke method(#WordApp.Quit) savechanges(0)

*

*Disable controls in the Word Application group box except for the Start Word button

set com(#showhidebtn #gpbx_1 #rdbn_1 #rdbn_2 #rdbn_3 #Addbtn #labl_1 #exitbtn) enabled(false)

*

*Disable controls in the Data for New Document group box

set com( #gpbx_3 #labl_2) enabled(false)

clr_list #ltvw_1

*Clear lists and disable controls in the Work with Document group box

clr_list #ltvw_2

set com(#gpbx_4 #savebtn #printbtn #closebtn) enabled(false)

execute infmessage 'Word has been closed.'

ENDROUTINE

 

EVTROUTINE HANDLING(#ADDBTN.Click)

*Add a new document and base it on the vl_sam99.dot Word template

USE BUILTIN(TCONCAT) WITH_ARGS(*PART_DIR_SOURCE VL_SAM099.DOT) TO_GET(#STD_QSEL)

invoke method(#WordApp.documents.add) template(#STD_QSEL) add_retval(#WordDoc)

*

*Add employee information to #ltvW_1

select fields(*all) from_file(pslmst)

add_entry #ltvw_1

endselect

*

*Enable and show controls in the Data for New Document group box

set com(#gpbx_3 #labl_2) enabled(true)

 

execute infmessage 'A new document has been opened.'

 

*Update the list of documents

execute doclist

 

*disable the Add Document button until the employee information has been inserted

set #addbtn enabled(false)

ENDROUTINE

 

*Add employee information to the document

EVTROUTINE HANDLING(#ltvw_1.DoubleClick)

*Concatenate first name and surname to get #Fullname

Use BConcat (#GiveName #SurName) (#FullName)

*Locate the Word bookmark 'fullname' in the document

invoke method(#WordDoc.Bookmarks.item<'fullname'>.select)

*Insert the value of the #fullname field

invoke method(#WordApp.Selection.TypeText) text(#fullname)

 

invoke method(#WordDoc.Bookmarks.item<'address1'>.select)

invoke method(#WordApp.Selection.TypeText) text(#address1)

 

invoke method(#WordDoc.Bookmarks.item<'address2'>.select)

invoke method(#WordApp.Selection.TypeText) text(#address2)

 

invoke method(#WordDoc.Bookmarks.item<'address3'>.select)

invoke method(#WordApp.Selection.TypeText) text(#address3)

 

invoke method(#WordDoc.Bookmarks.item<'postcode'>.select)

use numeric_string #postcode #std_texts

invoke method(#WordApp.Selection.TypeText) text(#std_texts)

 

invoke method(#WordDoc.Bookmarks.item<'phonehme'>.select)

invoke method(#WordApp.Selection.TypeText) text(#phonehme)

 

invoke method(#WordDoc.Bookmarks.item<'phonebus'>.select)

invoke method(#WordApp.Selection.TypeText) text(#phonebus)

 

invoke method(#WordDoc.Bookmarks.item<'writernam'>.select)

invoke method(#WordApp.Selection.TypeText) text(#WordApp.UserName)

 

invoke method(#WordDoc.Bookmarks.item<'homepage'>.select)

invoke method(#WordApp.Selection.TypeText) text(www.lansa.com)

 

invoke method(#WordDoc.Bookmarks.item<'fulldate'>.select)

invoke method(#WordApp.Selection.TypeText) text(#datec)

 

*Get the skills information for the employee and add it to the working list #skills

Select *all From_File(PslSkl) with_Key(#Empno)

Fetch #SkilDesc from_File(SklTab) With_Key(#SkilCode) Keep_Last(50)

Add_Entry #Skills

EndSelect

 

invoke method(#WordDoc.Bookmarks.item<'skills'>.select)

Selectlist #skills

*Insert the value of the #skilcode field

invoke method(#WordApp.Selection.TypeText) text(#skilcode)

*Move cursor right to the next table cell

invoke method(#wordapp.Selection.MoveRight) Unit(1) Count(1)

 

invoke method(#WordApp.Selection.TypeText) text(#skildesc)

invoke method(#wordapp.Selection.MoveRight) Unit(1) Count(1)

 

invoke method(#WordApp.Selection.TypeText) text(#grade)

invoke method(#wordapp.Selection.MoveRight) Unit(1) Count(1)

 

invoke method(#WordApp.Selection.TypeText) text(#comment)

invoke method(#wordapp.Selection.MoveRight) Unit(1) Count(1)

 

*Add a new row to the table

invoke method(#wordapp.Selection.InsertRows)

endselect

*Delete the last empty row of the table

invoke method(#wordapp.selection.Rows.Delete)

 

*Enable and disable components

set com(#gpbx_4 #ltvw_2 #savebtn #closebtn #printbtn) enabled(true)

set com(#gpbx_3 #labl_2) enabled(false)

clr_list #ltvw_1

set #addbtn enabled(true)

 

execute infmessage 'Employee details have been added to document.'

ENDROUTINE

 

EVTROUTINE HANDLING(#LTVW_2.ItemGotFocus)

*activate the selected document

invoke method(#wordapp.documents.item<#std_text>.activate)

 

*set #ltvw_2.currentitem selected(true)

ENDROUTINE

 

EVTROUTINE HANDLING(#PRINTBTN.Click)

*Print out the document.

invoke method(#WordDoc.PrintOut)

 

*Alternative methods:

* invoke method(#WordApp.Dialogs.item<VA_WORD.wdDialogFilePrint>.show)

* invoke method(#WordApp.ActiveDocument.PrintOut)

execute infmessage 'Document has been sent to the printer.'

ENDROUTINE

 

EVTROUTINE HANDLING(#SAVEBTN.Click)

*Open the Save dialog

invoke method(#WordApp.Dialogs.item<VA_WORD.wdDialogFileSaveAs>.show)

 

*update document list

execute doclist

execute infmessage 'Document has been saved.'

ENDROUTINE

 

EVTROUTINE HANDLING(#CLOSEBTN.Click)

*if a document is open, close it without saving any changes

if cond('#WordApp.Documents.Count *gt 0')

invoke method(#wordapp.activedocument.close) savechanges(0)

endif

execute doclist

execute infmessage 'Document has been closed.'

ENDROUTINE

 

SUBROUTINE NAME(DOCLIST)

*get the number of open Word documents

change #std_num #WordApp.Documents.Count

 

*Clear #ltvw_2 and then add the names of all open documents

clr_list #ltvw_2

begin_loop using(#i) FROM(1) TO(#std_num)

change #std_text #wordapp.documents.item<#i.value>.name_COM

add_entry #ltvw_2

end_loop

 

if cond('#std_num *gt 0')

*Set a reference to the currently active document into the variable #WordDoc

SET_REF COM(#WordDoc) TO(#WordApp.ActiveDocument)

endif

 

endroutine

 

* Information Message created by template VL_BBSTSBR

SUBROUTINE INFMESSAGE ((#EX_TEXT *RECEIVED))

define #ex_text *char 132

message msgid(dcm9993) msgf([email protected]) msgdta(#ex_text *blanks)

ENDROUTINE

END_COM