//TH-Header //***************************************************************************************** // Copyright (c) 2012 Antwise Solutions // All rights reserved. // // $FileName : cOutlook.pkg // $ProjectName : Order Entry Example // $Authors : Wil van Antwerpen // $Created : 11.28.2012 13:44 // $Type : Commercial // // Contents: // //***************************************************************************************** //TH-RevisionStart //TH-RevisionEnd // we use a heavily abbreviated version of the package so as not to create bloat // and to not make it clash with another copy of the outlook package and // other MS Office generated flexcom packages. //Use MSOUTL.pkg Use OutlookBase.pkg Use File_dlg.pkg Class cAttachmentSaveAsDialog is a SaveAsDialog Procedure Construct_Object Forward Send Construct_Object Set HideReadOnly_State To True End_Procedure // Construct_Object Function SelectedFileName Returns String String sFileName Move "" To sFileName If (Show_Dialog(Self)) Begin Move (RTrim(File_Name(Self))) To sFileName End Function_Return sFileName End_Function // SelectedFileName End_Class // cAttachmentSaveAsDialog Class cOutlookApplication is a cOutlookBaseApplication Function AttachActiveObject Returns Boolean Boolean bSuccess Variant vComObject String sProgId // Move False To bSuccess Send Ignore_Error of Error_Object_Id 4381 // Get psProgID to sProgId Get ComActiveObject sProgId to vComObject If (not(IsNullComObject(vComObject))) Begin Set pvComObject to vComObject Move (not(IsNullComObject(vComObject))) to bSuccess End // Send Trap_Error of Error_Object_Id 4381 // Function_Return bSuccess End_Function Procedure OnCreate Forward Send Oncreate Set peAutoCreate to acAutoCreate End_Procedure End_Class // cOutlookApplication Class cOutlookReader is a cObject Procedure Construct_Object Forward send Construct_Object End_Procedure // Construct_Object // // Sets up the connection with Outlook application object and attaches to it // // Returns the handle to the oApplication object // Function CreateOutlookConnection Returns Handle Handle hoApplication Boolean bIsComObjectCreated Get Create U_cOutlookApplication to hoApplication If (hoApplication) Begin // Is the VDF Wrapper already connected to Outlook? Get IsComObjectCreated of hoApplication To bIsComObjectCreated If (Not(bIsComObjectCreated)) Begin // Seems not, try to attach to a running instance of Outlook Get AttachActiveObject of hoApplication To bIsComObjectCreated If (Not(bIsComObjectCreated)) Begin // Hmm, no such luck. Oh well, let's create our own instance Send CreateComObject of hoApplication End End Get IsComObjectCreated of hoApplication To bIsComObjectCreated If (bIsComObjectCreated=false) Begin Send UserError "Unable to connect to Outlook.\nIs Outlook version 2003 or higher installed?" Move 0 To hoApplication End End Function_Return hoApplication End_Function // CreateOutlookConnection // // Setup the connection to the navigation panel on the left. // This also selects that you selected a current folder. // Function OpenExplorer Handle hoApplication Returns Handle Handle hoExplorer Handle hoFolders Variant vExplorer Variant vCurrentFolder Get Create U_cOutlookBaseExplorer to hoExplorer Get Create U_cOutlookBaseFolders to hoFolders If (hoExplorer and hoFolders) Begin Get ComActiveExplorer of hoApplication to vExplorer If (IsNullComObject(vExplorer)) Begin Send UserError "The navigation pane has been disabled or is invisible.\nThe navigation pane must be enabled for the drag and drop to work." Move 0 To hoExplorer End If (hoExplorer) Begin Set pvComObject of hoExplorer to vExplorer Get ComCurrentFolder of hoExplorer to vCurrentFolder If (IsNullComObject(vCurrentFolder)) Begin Send UserError "No Folder has been selected in the navigation pane.\nYou will need to select a folder in there for the drag and drop to work." Move 0 To hoExplorer End Else Begin Set pvComObject of hoFolders to vCurrentFolder // don't think we still need the folders? Send Destroy of hoFolders End End End Else Move 0 To hoExplorer Function_Return hoExplorer End_Function // OpenExplorer // // Returns the number of currently selected emails in outlook. // Function SelectedEmailCount Returns Integer Handle hoApplication Handle hoExplorer Handle hoSelection Integer iItemCount Variant vSelection Move 0 To iItemCount Get CreateOutlookConnection To hoApplication If (hoApplication) Begin Get OpenExplorer hoApplication to hoExplorer If (hoExplorer) Begin Get ComSelection of hoExplorer to vSelection Get Create U_cOutlookBaseSelection to hoSelection If (hoSelection) Begin Set pvComObject of hoSelection to vSelection Get ComCount of hoSelection to iItemCount Send Destroy of hoSelection End Send Destroy of hoExplorer End Send Destroy of hoApplication End Function_Return iItemCount End_Function // SelectedEmailCount // // Get the email subject for the selected item where the item // can be a number from 1 to iCount selected items // Procedure SaveEmail Integer iItem String sPath Handle hoApplication Handle hoExplorer Handle hoSelection Handle hoMailItem Integer iItemCount Variant vCurrentItem Variant vSelection Get CreateOutlookConnection To hoApplication If (hoApplication) Begin Get OpenExplorer hoApplication to hoExplorer If (hoExplorer) Begin Get ComSelection of hoExplorer to vSelection Get Create U_cOutlookBaseSelection to hoSelection If (hoSelection) Begin Set pvComObject of hoSelection to vSelection Get ComCount of hoSelection to iItemCount If (iItemCount>0 and iItem<=iItemCount) Begin Get Create U_cOutlookBaseMailItem to hoMailItem If (hoMailItem) Begin Get ComItem of hoSelection iItem to vCurrentItem Set pvComObject of hoMailItem to vCurrentItem Send ComSaveAs of hoMailItem sPath Nothing Send Destroy of hoMailItem End End Send Destroy of hoSelection End Send Destroy of hoExplorer End Send Destroy of hoApplication End End_Procedure // SaveEmail // // Get the email subject for the selected item where the item // can be a number from 1 to iCount selected items // Function SelectedEmailSubject Integer iItem Returns String Handle hoApplication Handle hoExplorer Handle hoSelection Handle hoMailItem Integer iItemCount Variant vCurrentItem Variant vSelection String sText Move "" To sText Get CreateOutlookConnection To hoApplication If (hoApplication) Begin Get OpenExplorer hoApplication to hoExplorer If (hoExplorer) Begin Get ComSelection of hoExplorer to vSelection Get Create U_cOutlookBaseSelection to hoSelection If (hoSelection) Begin Set pvComObject of hoSelection to vSelection Get ComCount of hoSelection to iItemCount If (iItemCount>0 and iItem<=iItemCount) Begin Get Create U_cOutlookBaseMailItem to hoMailItem If (hoMailItem) Begin Get ComItem of hoSelection iItem to vCurrentItem Set pvComObject of hoMailItem to vCurrentItem Get ComConversationTopic of hoMailItem to sText Send Destroy of hoMailItem End End Send Destroy of hoSelection End Send Destroy of hoExplorer End Send Destroy of hoApplication End Function_Return sText End_Function // SelectedEmailSubject // // Get the email html body for the selected item where the item // can be a number from 1 to iCount selected items // Function EmailHtmlBody Integer iItem Returns String Handle hoApplication Handle hoExplorer Handle hoSelection Handle hoMailItem Integer iItemCount String sHtmlBody Variant vCurrentItem Variant vSelection Move "" To sHtmlBody Get CreateOutlookConnection To hoApplication If (hoApplication) Begin Get OpenExplorer hoApplication to hoExplorer If (hoExplorer) Begin Get ComSelection of hoExplorer to vSelection Get Create U_cOutlookBaseSelection to hoSelection If (hoSelection) Begin Set pvComObject of hoSelection to vSelection Get ComCount of hoSelection to iItemCount If (iItemCount>0 and iItem<=iItemCount) Begin Get Create U_cOutlookBaseMailItem to hoMailItem If (hoMailItem) Begin Get ComItem of hoSelection iItem to vCurrentItem Set pvComObject of hoMailItem to vCurrentItem Get ComHTMLBody of hoMailItem To sHtmlBody Send Destroy of hoMailItem End End Send Destroy of hoSelection End Send Destroy of hoExplorer End Send Destroy of hoApplication End Function_Return sHtmlBody End_Procedure // EmailHtmlBody // // Gets the attachment count for the selected item where the item // can be a number from 1 to iCount selected items // Returns: // 0 = no attachment // 1 = 1 attachment // etc.. // Function EmailItemAttachmentCount Integer iItem Returns Integer Handle hoApplication Handle hoExplorer Handle hoSelection Handle hoMailItem Handle hoAttachments Integer iItemCount Integer iAttachmentsCount Variant vCurrentItem Variant vSelection Variant vAttachments Move 0 To iAttachmentsCount Get CreateOutlookConnection To hoApplication If (hoApplication) Begin Get OpenExplorer hoApplication to hoExplorer If (hoExplorer) Begin Get ComSelection of hoExplorer to vSelection Get Create (RefClass(cOutlookBaseSelection)) to hoSelection If (hoSelection) Begin Set pvComObject of hoSelection to vSelection Get ComCount of hoSelection to iItemCount If (iItemCount>0 and iItem<=iItemCount) Begin Get Create (RefClass(cOutlookBaseMailItem)) to hoMailItem If (hoMailItem) Begin Get ComItem of hoSelection iItem to vCurrentItem Set pvComObject of hoMailItem to vCurrentItem Get Create (RefClass(cOutlookBaseAttachments)) To hoAttachments If (hoAttachments) Begin Get ComAttachments of hoMailItem To vAttachments Set pvComObject of hoAttachments To vAttachments Get ComCount of hoAttachments To iAttachmentsCount Send Destroy of hoAttachments End Send Destroy of hoMailItem End End Send Destroy of hoSelection End Send Destroy of hoExplorer End Send Destroy of hoApplication End Function_Return iAttachmentsCount End_Function // EmailItemAttachmentCount // // Returns the variant needed for creating the attachment object // // Function EmailAttachment Integer iMailItem Integer iAttachment Returns Variant Handle hoApplication Handle hoExplorer Handle hoSelection Handle hoMailItem Handle hoAttachments Integer iItemCount Integer iAttachmentsCount Variant vCurrentItem Variant vSelection Variant vAttachment Variant vAttachments Move 0 To iAttachmentsCount Move (NullComObject()) To vAttachment Get CreateOutlookConnection To hoApplication If (hoApplication) Begin Get OpenExplorer hoApplication to hoExplorer If (hoExplorer) Begin Get ComSelection of hoExplorer to vSelection Get Create (RefClass(cOutlookBaseSelection)) to hoSelection If (hoSelection) Begin Set pvComObject of hoSelection to vSelection Get ComCount of hoSelection to iItemCount If (iItemCount>0 and iMailItem<=iItemCount) Begin Get Create (RefClass(cOutlookBaseMailItem)) to hoMailItem If (hoMailItem) Begin Get ComItem of hoSelection iMailItem to vCurrentItem Set pvComObject of hoMailItem to vCurrentItem Get Create (RefClass(cOutlookBaseAttachments)) To hoAttachments If (hoAttachments) Begin Get ComAttachments of hoMailItem To vAttachments Set pvComObject of hoAttachments To vAttachments Get ComCount of hoAttachments To iAttachmentsCount If (iAttachmentsCount>0 and iAttachment<=iAttachmentsCount) begin Get ComItem of hoAttachments iAttachment to vAttachment End Send Destroy of hoAttachments End Send Destroy of hoMailItem End End Send Destroy of hoSelection End Send Destroy of hoExplorer End Send Destroy of hoApplication End Function_Return vAttachment End_Function // EmailAttachment // // Html emails can have images embedded in the html. These images do show up as an attachment // yet they do not have appear to have a specific type to identify that they have been used. // My testing showed images both as "by value" where one as clearly embedded in the text. // So we detect it by ourself by searching in the html and see if they are referenced with // a content-ID (cid:) prefix. // Function EmailAttachmentIsEmbedded Integer iMailItem Integer iAttachment Returns Boolean Boolean bIsEmbedded Handle hoAttachment String sFileName //String sName String sHtmlBody String sCID Variant vAttachment Move False To bIsEmbedded Get EmailHtmlBody iMailItem to sHtmlBody If (sHtmlBody<>"") Begin Get EmailAttachment iMailItem iAttachment To vAttachment If (Not(IsNullComObject(vAttachment))) Begin Get Create (RefClass(cOutlookBaseAttachment)) To hoAttachment If (hoAttachment) Begin Set pvComObject of hoAttachment To vAttachment //Get ComDisplayName of hoAttachment To sName Get ComFileName of hoAttachment To sFileName Send Destroy of hoAttachment End End If (sFileName<>"") Begin Move ("cid:"+sFileName) To sCID // Uppercasing is probably not needed, but.. rather not find out later that // an emailprogram decided to uppercase things. If (Pos(Uppercase(sCID),Uppercase(sHtmlBody))>0) Begin Move True To bIsEmbedded End End End Function_Return bIsEmbedded End_Function // // This method is now obsolete it was just used to verify that the methods worked // and ran in a debugger. // Procedure EnumerateSelectedEmailSubjects Handle hoApplication Handle hoExplorer Handle hoSelection Handle hoMailItem Integer iItem Integer iItemCount Variant vCurrentItem Variant vSelection String sText Get CreateOutlookConnection To hoApplication If (hoApplication) Begin Get OpenExplorer hoApplication to hoExplorer If (hoExplorer) Begin Get ComSelection of hoExplorer to vSelection Get Create (RefClass(cOutlookBaseSelection)) to hoSelection If (hoSelection) Begin Set pvComObject of hoSelection to vSelection Get ComCount of hoSelection to iItemCount If (iItemCount>0) Begin For iItem from 1 To iItemCount Get Create (RefClass(cOutlookBaseMailItem)) to hoMailItem If (hoMailItem) Begin Get ComItem of hoSelection iItem to vCurrentItem Set pvComObject of hoMailItem to vCurrentItem //Get ComBody of oMailItem to sText // protected Get ComConversationIndex of hoMailItem to sText // available Get ComConversationTopic of hoMailItem to sText Send Destroy of hoMailItem End Loop End Send Destroy of hoSelection End Send Destroy of hoExplorer End Send Destroy of hoApplication End End_Procedure // RetrieveSelectedEmailSubjects End_Class // cOutlook