Reusable Part VL_SAM092: Interface to MS Word

LANSA

Reusable Part VL_SAM092: Interface to MS Word
Name: VL_SAM092

Description: The following RDMLX reusable component is used to by various MS Word examples as an interface to MS Word.
FUNCTION OPTIONS(*DIRECT);
BEGIN_COM DISPLAYPOSITION(1) HEIGHT(34) LEFT(0) TABPOSITION(1) TOP(0) WIDTH(49);
Define #LongChar *char 256 Desc('Long char work field');
Define #Char100 *char 100 Desc('Char 100 workField') ;
Define #ShortChar *char 50 Desc('Short char work field') ;
Define #ListName *char 20 Desc('Dynamic List Name') ;
Define #OsRc *dec 7 0 Desc('Operating System Error Code');
Define #WinRc *char 2 desc('Winword return code');
Define #DocDir *char 256 default(*Part_Dir_Source);
Define #DummyDoc *char 20 default('__Temp__.doc');
Define #LstFilSav *char 256 ;
;
Define_Pty Connected get(GetConnected) Help('Contains TRUE if an active link to MS Word currently exists, otherwise this property contains FALSE. ');
Define_Pty LastFileSaved get(GetLastSave) Help('Contains the fully qualified name of the last file saved. ');
;
PtyRoutine GetConnected;
Define_Map *output #Std_Texts #Prop001;
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc *eq ok';
Set #Prop001 Value(TRUE);
Else ;
Set #Prop001 Value(FALSE);
Endif ;
EndRoutine ;
;
PtyRoutine GetLastSave;
Define_Map *output #SysVar$Av #Prop002;
Set #Prop002 Value(#LstFilSav);
EndRoutine ;
;
Subroutine AddTemp ((#LongChar *Received)) ;
Add_Entry #TempList;
Endroutine ;
;
Subroutine StartWord;
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc *ne ok';
Use TConcat (#DocDir #DummyDoc) (#Char100);
Use Ov_File_Service (Check_File #Char100) (#WinRc);
if '#WinRc *ne ok';
Def_List #TempList (#LongChar) type(*Working) Entrys(20);
Execute AddTemp ('This is a temporary file used to start MS Word.');
Execute AddTemp ('Please ignore this file.');
Execute AddTemp ('Please do not use this file.');
use Transform_List (#TempList #Char100 C B T) to_get(#WinRc);
Endif ;
Use ov_winword (NoAutoStart) ;
Use tConcat (#DocDir #DummyDoc) (#LongChar) ;
Use System_Command with_args(H #longchar) to_get(#osrc);
if '#OsRc *ne 0';
Execute FatalError ('Unable to start MS-WORD');
Endif ;
Use ov_winword (Connect) (#WinRC);
if '#WinRC *ne ok';
Execute FatalError ('Unable to connect to start MS-WORD');
Endif ;
Endif ;
EndRoutine ;
;
MthRoutine MaximizeForm Help('Maximizes the MS Word instance you are currently connected to, otherwise the request is ignored.') ;
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc *eq ok';
Execute WinCmd ('APPMAXIMIZE "Microsoft Word",1') ;
Endif ;
EndRoutine ;
;
MthRoutine MinimizeForm Help('Minimizes the MS Word instance you are currently connected to, otherwise the request is ignored.');
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc *eq ok';
Execute WinCmd ('APPMINIMIZE "Microsoft Word",1') ;
Endif ;
EndRoutine ;
;
MthRoutine OpenWord Help('Opens an MS Word instance and connects you to it. If you are already connected the request is ignored.');
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc *ne ok';
Execute StartWord;
Endif ;
EndRoutine ;
;
MthRoutine CloseWord Help('Closes the MS Word instance you are currently connected to, otherwise the request is ignored.');
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc = ok';
Execute WinCmd ('APPCLOSE') ;
Use ov_winword (DisConnect);
Endif ;
EndRoutine ;
;
MthRoutine HideForm Help('Hides the MS Word instance you are currently connected to, otherwise the request is ignored.');
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc *eq ok';
Execute WinCmd ('APPHIDE "Microsoft Word"') ;
Endif ;
EndRoutine ;
;
MthRoutine RestoreForm Help('Restores the MS Word instance you are currently connected to, otherwise the request is ignored.');
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc *eq ok';
Execute WinCmd ('APPRESTORE') ;
Endif ;
EndRoutine ;
;
MthRoutine ShowForm Help('Shows the MS Word instance you are currently connected to, otherwise the request is ignored.');
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc *eq ok';
Execute WinCmd ('APPSHOW') ;
Endif ;
EndRoutine ;
;
MthRoutine OpenDoc Help('Opens the specified document (which must exist) in MS Word..');
Define_Map *input #Std_Texts #Named Help('Name of document to be opened. You should include the document suffix (eg: .doc, .htm, etc).');
Execute StartWord;
Use TConcat ('FILEOPEN .NAME="' #DocDir #Named.Value '"' ) (#LongChar);
Execute WinCmd (#LongChar) ;
EndRoutine ;
;
MthRoutine NewDoc Help('Creates a new document.');
Define_Map *input #Std_Texts #Template Help('The name of the template to be used to create a new document. Do NOT include the .DOT suffix in the name.');
Execute StartWord;
Use TConcat ('FILENEW .TEMPLATE="' #DocDir #Template.Value '.dot"') (#LongChar);
Execute WinCmd (#LongChar) ;
EndRoutine ;
;
MthRoutine InsertValues Help('Inserts field values into bookmarked text form fields in the current document.');
Define_Map *input #SysVar$Av #FromFields ('Specifies the name(s) of the field(s) whose value(s) are to be inserted. Delimit fields in this parameter by using a comma (",") between the names. The fields specified must exist in your function and must be validly defined as bookmarked text form fields in the receiving MW Word document. The bookmark name used for each text form field should be the same as the name of the LANSA field.');
Execute StartWord;
Use ov_winword (Insert_Fields_Ext #FromFields.Value Y);
EndRoutine ;
;
MthRoutine Execute;
Define_Map *input #SysVar$Av #Command ('Specifies the MS-WORD command to be executed');
Execute StartWord;
Use ov_winword (Command #Command.Value Y);
EndRoutine ;
;
MthRoutine InsertTable Help('Inserts values from a working list into a MW Word table in the current document.');
Define_Map *input #Std_TextS #FromList Help('The name of the working list containing the values to be inserted into the MS Word table. If this list is empty (ie: zero entries) then the receiving MS Word table will be deleted from the document. The MS-Word table must have a text form field bookmarked with this list name in the cell at which the insertion should start. Normally the leftmost cell in row 1 or 2 of the MW Word table is used for this purpose.') ;
Define_Map *input #SysVar$Av #FromFields ('Specifies the name(s) of the field(s) from the specified list whose value(s) are to be inserted into the MS Word table. Delimit fields in this parameter by using a comma (",") between the names. The fields specified must exist in as columns in the specified list. The number of fields specified in this parameter should exactly equal the number of cells in a single row of the receiving MS Word table.');
Execute StartWord;
Use TConcat ('LIST_' #FromList.Value) (#ListName) ;
Use ov_winword (#ListName #FromFields.Value Y);
EndRoutine ;
;
MthRoutine PrintDoc Help('Prints the current document if an active link to a MS Word instance exists, otherwise this request is ignored.');
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc *eq ok';
Execute WinCmd ('FILEPRINT') ;
Endif ;
EndRoutine ;
;
MthRoutine SendMailDoc Help('Prepares and prompts sending the current document as as mail if an active link to MS Word exists, otherwise this request is ignored. This method usually only functions with MS mail products.');
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc *eq ok';
Execute WinCmd ('FILESENDMAIL') ;
Endif ;
EndRoutine ;
;
MthRoutine CloseDoc Help('Closes the current document if an active link to a MS Word instance exists, otherwise this request is ignored.');
Define_Map *input #Std_Texts #Option Help('Must be specified as one of Prompt, Save or Discard to indicate what MS Word should do when closing unsaved but updated (ie: dirty) documents.') ;
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc *eq ok';
Execute StartWord;
Use UpperCase (#Option.Value) (#ShortChar);
Change #LongChar 'FILECLOSE' ;
Case #ShortChar;
When '= Prompt' ;
Use BConcat (#LongChar '0') (#LongChar) ;
When '= Save' ;
Use BConcat (#LongChar '1') (#LongChar) ;
When '= Discard';
Use BConcat (#LongChar '2') (#LongChar) ;
otherwise ;
Execute FatalError ('Invalid value passed in Options parameter');
EndCase ;
Execute WinCmd (#LongChar) ;
Endif ;
EndRoutine ;
;
MthRoutine CloseAllDoc Help('Closes all open documents if an active link to a MS Word instance exists, otherwise this request is ignored.') ;
Define_Map *input #Std_Texts #Option Help('Must be specified as one of Prompt, Save or Discard to indicate what MS Word should do when closing unsaved but updated (ie: dirty) documents.') ;
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc = ok';
Use UpperCase (#Option.Value) (#ShortChar);
Change #LongChar 'FILECLOSEALL' ;
Case #ShortChar;
When '= Prompt' ;
Use BConcat (#LongChar '0') (#LongChar) ;
When '= Save' ;
Use BConcat (#LongChar '1') (#LongChar) ;
When '= Discard';
Use BConcat (#LongChar '2') (#LongChar) ;
otherwise ;
Execute FatalError ('Invalid value passed in Options parameter');
EndCase ;
Execute WinCmd (#LongChar) ;
Endif ;
EndRoutine ;
;
MthRoutine SaveDoc Help('Saves the current document if an active link to a MS Word instance exists, otherwise this request is ignored.');
Define_Map *input #Std_Texts #WithName Help('Specifes the name to be used when saving the document. Specify the suffix (eg: .doc, .htm, etc) as part of this name.') ;
Define_Map *input #Std_Texts #InFormat mandatory(MSWORD) Help('Specifies the format the document is to be saved in. Valid formats are MSWORD (the default), TEXT, HTML and RTF.') ;
Use ov_winword (QueryConnection) (#WinRc);
If '#WinRc = ok';
Use TConcat (#DocDir #WithName.Value) (#LstFilSav) ;
Use TConcat ('FILESAVEAS .NAME="' #LstFilSav '"') (#LongChar);
Use UpperCase (#InFormat.Value) (#ShortChar);
Case #ShortChar;
When '= TEXT';
Use BConcat (#LongChar ',.FORMAT=5' ) (#LongChar) ;
When '= MSWORD' ;
Use BConcat (#LongChar ',.FORMAT=0' ) (#LongChar) ;
When '= HTML' ;
Use BConcat (#LongChar ',.FORMAT=8' ) (#LongChar) ;
When '= RTF';
Use BConcat (#LongChar ',.FORMAT=6' ) (#LongChar) ;
otherwise ;
Execute FatalError ('Invalid value passed in InFomat parameter');
EndCase ;
Execute WinCmd (#LongChar) ;
Endif ;
EndRoutine ;
;
Subroutine WinCmd ((#command *received));
Define #Command *char 256 ;
Use ov_winword (Command #command Y) (#WinRc);
If '#WinRc *ne Ok';
Execute FatalError ('Error detected when communicating with MS-WORD');
Endif ;
Endroutine ;
;
Subroutine FatalError ((#MsgText *Received));
Define #MsgText Reffld(#SysVar$Av);
Use Message_Box_Show (Ok Ok Warning *COMPONENT #MsgText);
Abort 'Fatal Error detected in MS Word reusable part interface' ;
EndRoutine ;
;
END_COM ;