An alternative way to send mails using Visual LANSA with Microsoft Outlook

Date:16 March 2005
Product/Release:Visual LANSA
Abstract:How to send mails using Visual LANSA without having to open the Microsoft Outlook window
Submitted By:LANSA Technical Support
Last Review:November 2010

Description:

You can send mails using SEND_MAIL BIFs in Visual LANSA but it requires Microsoft Outlook to be started.

There another alternative way to send emails without the need to ensure Microsoft Outlook window is opened.

Solution:

There is another way to send mails using Outlook ActiveX control in Visual LANSA V10.

The Outlook ActiveX does not require Outlook to be started, and it will start Outlook by itself. This method is somewhat more complicated though and it requires a good understanding of ActiveX programming.

Below is an example to demonstrate how to interact with an Outlook ActiveX Control. To use this form you need to create an ActiveX for Outlook called ---> VAOUTLOOK.

Note: If you have Outlook 2000, then the TypeLibID will not load when you select the ProgID. Hence in order to use Outlook 2000 ActiveX(OCX), you will need to specify the TypeLibID manually:

  1. For ProgID, select Outlook.Application.9 (Microsoft Outlook from the list of 'Other Components').
  2. The TypeLibID will be blank, so you need to select Microsoft Outlook 9 Object Library - version 9.0.
  3. The completed component should look like this:

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

If you use Outlook XP you can ignore the instructions above (i.e. selecting the ProgID)

The sample code:

FUNCTION OPTIONS(*DIRECT)
BEGIN_COM ROLE(*EXTENDS #PRIM_FORM) CLIENTHEIGHT(115) CLIENTWIDTH(318) HEIGHT(149) 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(4) TOP(8) WIDTH(297)
DEFINE_COM CLASS(#STD_TEXT.Visual) NAME(#recipient) CAPTION('To:') DISPLAYPOSITION(1) HEIGHT(19) LABELTYPE(Caption) LEFT(8) MARGINLEFT(80) PARENT(#COM_OWNER) TABPOSITION(1) TOP(40) USEPICKLIST(False) WIDTH(300)
DEFINE_COM CLASS(#STD_TEXT.Visual) NAME(#subject) CAPTION('Subject: ') DISPLAYPOSITION(3) HEIGHT(19) LABELTYPE(Caption) LEFT(8) MARGINLEFT(80) PARENT(#COM_OWNER) TABPOSITION(2) TOP(64) USEPICKLIST(False) WIDTH(300)
DEFINE_COM CLASS(#STD_TEXT.Visual) NAME(#bodytext) CAPTION('Message Body:') DISPLAYPOSITION(4) HEIGHT(19) LABELTYPE(Caption) LEFT(8) MARGINLEFT(80) PARENT(#COM_OWNER) TABPOSITION(3) TOP(88) USEPICKLIST(False) WIDTH(300)
EVTROUTINE HANDLING(#com_owner.Initialize)
SET COM(#com_owner) CAPTION(*component_desc)
ENDROUTINE

EVTROUTINE HANDLING(#SendEmail.Click)

* Sanity Check
If Cond('(#recipient *eq *Blanks) *or (#Subject *eq *Blanks) *or (#BodyText *eq *Blanks)')
Use Builtin(Message_Box_Add) With_Args('Please fill out all fields 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(#Subject)

* Set the body text
SET COM(#olMailItem) Body(#BodyText)

* 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 #Subject #BodyText) To(*Blanks)

* 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