Inserting Carriage Returns in a string

Date:Archived
Product/Release:Visual LANSA V10
Abstract:Multiline Editbox can be used to format a string for output
Submitted By:LANSA Technical Support

Description:

An alternative way to send emails using Visual LANSA with Microsoft Outlook describes how you can make use of the Outlook ActiveX for greater control and flexibility in sending emails. The Outlook ActiveX control has a single property which contains the body text of the email (Unlike the LANSA email BIFs which allow you to add each line of the email one by one). To send a multi-line email using this ActiveX you need to insert Carriage Returns into the field that you will put in this property. This tip highlights how to insert a carriage return into a LANSA string/alpha field.

Solution:

There are 2 possible solutions to this in V10

The first solution uses the To_Overlay field definition parameter to put a Carriage Return into a character field, which can then be concatenated to other character fields. However, you will have a maximum field length of 256 characters on your email message. But this can be still useful for other controls or functionality

For example you would define your field as:

Define Field(#dec20) Type(*dec) Length(2) Decimals(0) Default(-10)
Define Field(#C_Return) Type(*char) Length(1) To_Overlay(#dec20 2)

The char field overlays the second half of the hexadecimal value of -10 stored in the decimal field, which happens to be the hexadecimal for Carriage Return. Then you can concatenate this carriage return onto other strings:

Use Builtin(tconcat) With_Args('Hello there' #C_Return) To_Get(#Std_Textl)
Use Builtin(tconcat) With_Args(#Std_textl 'I am a new line' #C_Return) To_Get(#Std_Textl)
Use Builtin(tconcat) With_Args(#Std_textl 'I am another new line' #C_Return) To_Get(#Std_Textl)
Use Builtin(tconcat) With_Args(#Std_textl 'I am the final line' #C_Return) To_Get(#Std_Textl)
Use Builtin(message_box_show) With_Args(ok ok info *Component #Std_Textl)

The second solution does not have a 256 character limit and it involves using a Multiline Edit Box. This is similar to a regular list box in design and functionality, however at runtime the user can type as if it were a text box. In the program you treat each line as a separate item in a list - You can add/edit/delete rows in the list. The part we are interested in is the OnOutput and OutputText properties. OutputText gives the contents of the edit box in one continuous string. OnOutput determines what is placed at the end of each line - Carriage Return, Line Feed, Space, Return etc.

By adding the lines of the intended email text as entries in the list (first line is added as first entry, then the second line is added etc), you can then choose to have line feeds inserted after each line when you pass the contents to the email ActiveX. For aesthetic purposes you can make the edit box invisible or hide it off the form.

Here is an example of how this can be done (this example uses the Outlook ActiveX to send an email):

Outlook ActiveX:

FUNCTION OPTIONS(*DIRECT)
BEGIN_COM ROLE(*EXTENDS #PRIM_MCCO) PROGID('Outlook.Application.9') TYPELIBID('{00062FFF-0000-0000-C000-000000000046}9.0,0')
END_COM

Form Source:

FUNCTION OPTIONS(*DIRECT)
BEGIN_COM ROLE(*EXTENDS #PRIM_FORM) CLIENTHEIGHT(66) CLIENTWIDTH(318) HEIGHT(100) LEFT(423) TOP(205) WIDTH(326)
* Required Outlook Objects
DEFINE_COM CLASS(#VAOUTLOOK.Application) NAME(#olApp) REFERENCE(*dynamic)
DEFINE_COM CLASS(#vaOUTLOOK._NameSpace) NAME(#olNS) REFERENCE(*DYNAMIC)
DEFINE_COM CLASS(#vaOUTLOOK.IDispatch) NAME(#olDispatch) REFERENCE(*DYNAMIC)
DEFINE_COM CLASS(#VAoutlook._MailItem) NAME(#olMailItem) REFERENCE(*dynamic)

* Visual Components
DEFINE_COM CLASS(#PRIM_PHBN) NAME(#SendEmail) CAPTION('Send Email') DISPLAYPOSITION(2) LEFT(8) PARENT(#COM_OWNER) TABPOSITION(2) TOP(32) WIDTH(300)
DEFINE_COM CLASS(#STD_TEXT.Visual) NAME(#recipient) CAPTION('To:') DISPLAYPOSITION(1) HEIGHT(19) LABELTYPE(Caption) LEFT(8) MARGINLEFT(30) PARENT(#COM_OWNER) TABPOSITION(1) TOP(8) USEPICKLIST(False) WIDTH(300)
DEFINE_COM CLASS(#PRIM_MEMO) NAME(#MEMO_1) COMPONENTVERSION(1) CURRENTLINE(1) DISPLAYPOSITION(3) HEIGHT(137) HORIZONTALSCROLL(False) LEFT(48) MAXIMUMLINELENGTH(50) ONOUTPUT(AddNewLine) PARENT(#COM_OWNER) TABPOSITION(3) TOP(80) WIDTH(217)
DEFINE_COM CLASS(#PRIM_MECL) NAME(#MECL_1) COLUMNROLE(Data) DISPLAYPOSITION(1) PARENT(#MEMO_1) SOURCE(#STD_TEXT) WIDTH(20)

EVTROUTINE HANDLING(#com_owner.Initialize)
SET COM(#com_owner) CAPTION(*component_desc)
ENDROUTINE

EVTROUTINE HANDLING(#SendEmail.Click)

* Sanity Check
If Cond('(#recipient *eq *Blanks)')

Use Builtin(Message_Box_Add) With_Args('Please enter a recipient before pressing Send')
USE BUILTIN(MESSAGE_BOX_SHOW) WITH_ARGS(OK OK INFORMATION INFORMATION)

Else
*
* Create Outlook object
*
SET_REF COM(#olApp) TO(*create_as #VAOUTLOOK.Application)
INVOKE METHOD(#olApp.CreateObject) OBJECTNAME(outlook.application)
INVOKE METHOD(#olApp.GetNamespace) TYPE(MAPI) GETNAMESPACE_RETVAL(#OLNS)
INVOKE METHOD(#olNS.Logon) SHOWDIALOG(1)

*
* Send Mail
*
* Create a new email and refer #olMailItem to it
INVOKE METHOD(#olApp.CreateItem) ITEMTYPE(vaoutlook.olMailItem) CREATEITEM_RETVAL(#olDispatch)
SET_REF COM(#olMailItem) TO(*dynamic #olDispatch)

* Set the 'To' field of the email (recipient)
SET COM(#olMailItem) TO(#Recipient)

* Set the Subject
SET COM(#olMailItem) SUBJECT('Test Email from LANSA Support')

* Set the body text
* The Memo box is regarded as a list - each line in the memo box is a new item
*
* Clear the memo
clr_list named(#Memo_1)
* 1st line
change #std_text 'Dear Recipient,'
add_entry to_list(#Memo_1)
* 2nd line
change #std_text 'Thankyou for reading this email.'
add_entry to_list(#Memo_1)
* 3rd line
change #std_text *blanks
add_entry to_list(#Memo_1)
* 4th line
change #std_text 'Regards,'
add_entry to_list(#Memo_1)
* 5th line
change #std_text 'Your Friendly LANSA Support Team Member'
add_entry to_list(#Memo_1)

* Add the contents of the memo box to the body component of the email ActiveX
* Because the OnOutput property of the multiline edit box is set to AddNewLine, a CRLF is inserted after each line
SET COM(#olMailItem) Body(#Memo_1.OutputText)

* Send out message and clean up...
INVOKE METHOD(#olMailItem.Send)

Set_Ref Com(#olNS) to(*Null)
Set_Ref Com(#olMailItem) to(*Null)
Change Field(#Recipient) To(*Blanks)
clr_list named(#Memo_1)

* All done...
Use Builtin(Message_Box_Add) With_Args('Message Sent .....')
USE BUILTIN(MESSAGE_BOX_SHOW) WITH_ARGS(OK OK INFORMATION INFORMATION)
EndIf
ENDROUTINE
End_Com