Accessing the outlook address book

Status
Not open for further replies.

Emma

Member
Hi all,

Does anyone know how i can access the outlook address book from within a progress program using com objects? Code examples appreciated.

Thanks in anticipation,

Emma. :confused:
 

cup99

New Member
The following code will go through every entry in the outlook contacts folder and message the name and email address.

DEFINE VARIABLE hOutlook AS COM-HANDLE NO-UNDO.
DEFINE VARIABLE hNameSpace AS COM-HANDLE NO-UNDO.
DEFINE VARIABLE hFolder AS COM-HANDLE NO-UNDO.
DEFINE VARIABLE hItem AS COM-HANDLE NO-UNDO.
DEFINE VARIABLE X AS INTEGER NO-UNDO.

CREATE "Outlook.Application" hOutlook.

hNameSpace = hOutlook:GetNameSpace("MAPI").
hFolder = hNameSpace:AddressLists:ITEM(1).

DO X = 1 TO hFolder:AddressEntries:COUNT.
hItem = hFolder:AddressEntries:ITEM(X).
MESSAGE hItem:NAME + " " hItem:Address.
END.

RELEASE OBJECT hItem.
RELEASE OBJECT hFolder.
RELEASE OBJECT hNameSpace.
RELEASE OBJECT hOutlook.

Do you want to be able to access the personal address book instead of the contacts folder?
 

GordonRobertson

New Member
Here's the code I use to let the user pick addresses from the Outlook address book. Hope it helps.

Gordon

Code:
RETURNS LOGICAL
  ( INPUT-OUTPUT iop-Email AS CHARACTER ) :
/*------------------------------------------------------------------------------
  Purpose:  Passed in an email address, fires up the address book to allow the
            user to select an email address(es). Returns updated list of addressed
            
    Notes:  
------------------------------------------------------------------------------*/


    DEFINE VARIABLE h-Session           AS COM-HANDLE NO-UNDO.
    DEFINE VARIABLE h-Recipients        AS COM-HANDLE NO-UNDO.
    DEFINE VARIABLE h-Recipient         AS COM-HANDLE NO-UNDO.
    DEFINE VARIABLE h-Message           AS COM-HANDLE NO-UNDO.
    DEFINE VARIABLE h-DefaultRecipients AS COM-HANDLE NO-UNDO.
    DEFINE VARIABLE h-DefaultRecipient  AS COM-HANDLE NO-UNDO.
    DEFINE VARIABLE v-Selected          AS LOGICAL    NO-UNDO.
    DEFINE VARIABLE v-Count             AS INTEGER    NO-UNDO.
    DEFINE VARIABLE v-Email             AS CHARACTER  NO-UNDO.


    /* Get a session */
    assign h-Session = f-MAPIGetSession().

    /* Handle failure */
    IF NOT VALID-HANDLE(h-Session) THEN
        RETURN FALSE.

    IF iop-Email <> "" THEN
    DO:

        /* If we've been passed in email addresses, we need to create a recipients
           collection to use as the default value for the addressbook call */

        /* Create our dummy message */
        h-Message = h-Session:outbox:messages:add.
        h-DefaultRecipients = h-Message:Recipients.

        /* Loop through the addresses */
        DO v-Count = 1 TO NUM-ENTRIES(iop-Email,";"):
    
            /* Create a recipient */
            h-DefaultRecipient = h-DefaultRecipients:Add.
            h-DefaultRecipient:name = ENTRY(v-Count,iop-Email,";").
            h-DefaultRecipient:Type = 1.                            /* To */
    
        END.

        h-DefaultRecipients:resolve().

        /* The address book returns a recipients collection */
        h-Recipients = h-Session:addressbook(h-DefaultRecipients) NO-ERROR.
  
    END.

    ELSE
        /* Otherwise call the addressbook without any defaults */
        h-Recipients = h-Session:addressbook() NO-ERROR.



    /* If the user cancelled */
    IF NOT VALID-HANDLE(h-Recipients) THEN
        ASSIGN v-Selected = FALSE.

    ELSE
    DO:
        
        ASSIGN v-Selected = TRUE.

        /* The user may have selected a number of addresses */
        DO v-Count = 1 TO h-Recipients:COUNT:

            /* Get the individual recipient */
            ASSIGN h-Recipient = h-Recipients:ITEM(v-Count).

            /* and take a note of the address */
            ASSIGN v-Email = v-Email +
                             ( IF v-Email = "" THEN ""
                               ELSE "; " ) +
                             h-Recipient:NAME.

        END.

    END.

    /* Delete the dummy message */
    h-Message:DELETE NO-ERROR.

    /* Logoff */
    h-Session:logoff.

    /* Tidy up */
    RELEASE OBJECT h-DefaultRecipient NO-ERROR.
    RELEASE OBJECT h-DefaultRecipients NO-ERROR.    
    RELEASE OBJECT h-Message NO-ERROR.
    RELEASE OBJECT h-Recipient NO-ERROR.
    RELEASE OBJECT h-Recipients NO-ERROR.
    RELEASE OBJECT h-Session NO-ERROR.

    ASSIGN iop-Email = v-Email.

    RETURN v-Selected.

END FUNCTION.
 
Status
Not open for further replies.
Top