Tech Articles


Reading MS Outlook Items


Here is some code which reads data from Outlook (2007 was tested) into Powerbuilder. You basically need to create a window with a multiline edit and a button on it. Put this into the clicked event of the button.

To run it, open Outlook, select something (email message, task, etc.) then click the button on your PB window. There are many, many more methods and properties you have access to from PB via OLE to Outlook. The MSDN reference online is a big help

integer li_rc
long ll_itemcount, ll_i
oleobject lole_item, lole_outlook, lole_exp, lole_selecteditems
string ls_subject, ls_from, ls_to, ls_body, ls_msg
lole_outlook = CREATE oleobject
lole_exp = CREATE oleobject
lole_selecteditems = CREATE oleobject
li_rc = lole_outlook.ConnectToNewObject("outlook.application")
lole_exp = lole_outlook.ActiveExplorer()
// Outlook has to be running
If IsNull(lole_exp) THEN
    Messagebox('Outlook Error','Is Outlook currently running?')
    GOTO cleanup
END IF
li_rc = lole_exp.class
// caption is window name like "Inbox - Microsoft Outlook" or "Calendar - Microsoft Outlook"
ls_subject = lole_exp.caption

lole_selecteditems = lole_exp.selection
ll_itemcount = lole_selecteditems.count
FOR ll_i = 1 to ll_itemcount
    lole_item = CREATE oleobject
    lole_item = lole_exp.selection.item(ll_i)
    li_rc = lole_item.class
    CHOOSE CASE li_rc
        CASE 26 //appointment
            ls_body = lole_item.body
            ls_msg += '~r~n Appointment No: ' + string(ll_i) + ' of ' + string(ll_itemcount) + '~r~n' + ls_body
            // lots of other stuff could be here
        CASE 40 // contact
            ls_body = lole_item.body
            ls_msg += '~r~n Contact No: ' + string(ll_i) + ' of ' + string(ll_itemcount) + '~r~n' + ls_body
            
        CASE 43 // mail
            ls_subject = lole_item.subject
            ls_from = lole_item.sendername
            ls_to = lole_item.to
            ls_body = lole_item.body
            ls_msg = '~r~nSubject: ' + ls_subject + '~r~nFrom: ' + ls_from + '~r~nTo: ' + ls_to + '~r~nBody: ' + ls_body
            ls_msg += '~r~n Email No: ' + string(ll_i) + ' of ' + string(ll_itemcount) + '~r~n' + ls_msg
            
        CASE 48 // task
            ls_subject = lole_item.subject
            ls_to = lole_item.owner
            ls_body = lole_item.body
            ls_msg = '~r~nSubject: ' + ls_subject + '~r~nOwner: ' + ls_to + '~r~nBody: ' + ls_body
            ls_msg += '~r~n Task No: ' + string(ll_i) + ' of ' + string(ll_itemcount) + '~r~n' + ls_msg
            
    END CHOOSE
    DESTROY lole_item
NEXT
IF Len(ls_msg) > 0 THEN
    mle_1.text = ls_msg
ELSE
    mle_1.text = 'No items processed.'
END IF
cleanup:
DESTROY lole_selecteditems
DESTROY lole_exp
DESTROY lole_outlook
Comments (1)
Thursday, Apr 20 2017

If you also want to do anything that changes anything in more recent versions of Outlook (like create emails, as one of my programs does) you will run into the problem where Outlook prompts you to allow an external program to make changes, for X minutes. Very annoying. The solution is to use the Outlook Redemption object instead, from <a href="http://www.dimastr.com/redemption">http://www.dimastr.com/redemption.<;/a> It's a free library that duplicates most of the Outlook OLE API, but uses a lower-level API to do the actual work, eliminating that security prompt.

#3
0

Find Articles by Tag

Event Handling RibbonBar DLL 64-bit ODBC Error BLOB iOS Windows OS PowerBuilder (Appeon) Debugging UI Modernization Migration Trial Filter TFS OrcaScript Web Service Proxy Icon Messagging InfoMaker OAuth 2.0 Windows 10 Icons DevOps PowerScript (PS) Export Mobile UI Themes Window License CrypterObject ActiveX Excel IDE SQL Server HTTPClient COM Source Control Service Database Table Schema SqlExecutor .NET Std Framework RichTextEdit Control Elevate Conference MessageBox OLE XML Transaction Azure Database Table Data GhostScript TortoiseGit External Functions Design Bug REST Array Database Profile Debugger Repository Jenkins Resize Charts 32-bit Performance Graph Model Oracle Automated Testing PostgreSQL ODBC driver Sort Text Installation Encryption Testing PFC Export JSON Linux OS Menu Android Configuration PowerBuilder Compiler .NET Assembly Git TreeView C# Expression Stored Procedure Import SVN PostgreSQL Debug JSONGenerator RibbonBar Builder DataWindow SnapObjects Authorization API DragDrop Database Connection WebBrowser TLS/SSL Open Source OAuth Source Code Database Object File PDF Event Handler Database Table Outlook CI/CD SOAP Class CoderObject PowerServer Mobile SQL PDFlib NativePDF Import JSON JSON Branch & Merge Application Database Painter Platform SDK Encoding Variable PowerBuilder DataWindow JSON Interface Event SqlModelMapper Validation Script Database Web API Data PBDOM UI SnapDevelop PowerServer Web .NET DataStore Authentication JSONParser DataType RESTClient WinAPI Syntax Deployment