part 4 – looking at the mailitem methods and events

 

in this part we will investigate the methods and events of the mail item object.

 

so let’s dive straight in and look at what methods we have available to us:

 

method

description

clearconversationindex

clears the index of the conversation thread for the mail message or post.

close (*)

close the mailitem, also note that there is an event for this method.

copy

allows you to copy the mail item from one folder to another

delete

delete the mailitem

display

calling this method will show the outlook mail item dialog

forward (*)

executes the forward action for a mail item

move

allows you to move the mail item from one folder to another

printout

the printout method is the only outlook method that can be used for printing.

reply

creates a reply, pre-addressed to the original sender, from the original message. returns the reply as a mailitem object

replyall

creates a reply to all original recipients from the original message. returns the reply as a mailitem object.

save

saves the microsoft outlook item to the current folder or, if this is a new item, to the outlook default folder for the item type.

saveas

saves the microsoft outlook item to the specified path and in the format of the specified file type. if the file type is not specified, the msg format (.msg) is used.

send (*)

send the mail item

showcategoriesdialog

displays the show categories dialog box, which allows you to select categories that correspond to the subject of the item

 

method: clearconversationindex

not too sure what exactly this does so if anybody could shed some light on this, this would be helpful, thanks.

 

example:

lomailitem.clearconversationindex()

 

method: close

close the current item; pass the appropriate parameter to either ask for save, or just discard without prompting, also you can save without prompting as well.

 

call: close(savemode)

where savemode =            0 – olsave = ‘save without prompting’

                                                1 – oldiscard = ‘discard all changes without prompting’

                                                2 – olpromptforsave = ‘prompt to save or discard changes’

 

example:

local loout  as outlook.application

local lons   as outlook.namespace

local lofold as outlook.folders

local lomail as outlook.mailitem

 

#define oldiscard          1

#define olpromptforsave    2

#define olsave                    0

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

lomail = loout.createitem(0)      && mailitem

 

lomail.subject = "test for close method"

lomail.display()

lomail.close(olpromptforsave)


 

method: copy

copy the current item to a new object.

 

the example code shows a few of the methods in action, it shows how to use the copy method and the move method; also we call the save to make sure this is saved into the new folder.

 

this example also shows you how to create a new mail folder in outlook using the folders collection.

 

example:

local loout  as outlook.application

local lons   as outlook.namespace

local lofold as outlook.mapifolder

local lonew  as outlook.mapifolder

local loitem as outlook.mailitem

local locopy as outlook.mailitem

 

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

lofold = lons.getdefaultfolder(6) && inbox

lonew  = lofold.folders.add("saved mail",16)    && olfolderdrafts

loitem = loout.createitem(0)      && new mail item

loitem.subject = "test for copy into new folder."

locopy = loitem.copy()

locopy.move(lonew)   && copy this into the new folder we created.

locopy.save

 

 

method: delete

deletes the current selected object, this could be a mailitem, folder etc.

 

this example assumes you ran the copy example first and did not manually remove these items from outlook.

 

example:

local loout  as outlook.application

local lons   as outlook.namespace

local lofold as outlook.mapifolder

local lonew  as outlook.mapifolder

local loitem as outlook.mailitem

 

       lofold = lons.getdefaultfolder(6) && new folder is under inbox.

lonew  = lofold.folders("saved mail")    && got reference to new folder.

loitem = lonew.items(1)           && got the single mail item.

lcmsg  = "delete message with subject: " + loitem.subject

if messagebox(lcmsg,36+256,"delete mail message") = 6

              *-- in outlook 2003, need to have a '=' sign otherwise

              *-- you will get an error saying parameter is not optional.

              =loitem.delete

              *-- notice the same method used to remove the folder as well.

              *-- clean up - remove the new folder.

              =lonew.delete

endif

 

 


 

method:                display

parameters:            optional – make window modal

this method allows you display a selected object; this can be a mailitem, contactitem, etc.

example: show a mail item

                local loout  as outlook.application

local lons   as outlook.namespace

local lofold as outlook.mapifolder

local loitem as outlook.mailitem

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

lofold = lons.getdefaultfolder(6) && inbox

loitem = lofold.items(1)          && get the first item

=loitem.display(.t.)              && display as modal dialog

 

example: show the deleted items folder

local loout  as outlook.application

local lons   as outlook.namespace

local lofold as outlook.mapifolder

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

lofold = lons.getdefaultfolder(3) && deleted items

lofold.display                    && display outlook with this folder selected.

 

method: forward

this method executes a forward action for the item.

 

example:

local loout  as outlook.application

local lons   as outlook.namespace

local lofold as outlook.mapifolder

local loitem as outlook.mailitem

local loforw as outlook.mailitem

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

lofold = lons.getdefaultfolder(6) && inbox

loitem = lofold.items(1)          && pick first item

loforw = loitem.forward           && create a new mailitem from selected.

with loforw

       .recipients.add("fred bloggs") && will cause outlook security prompt.

       .display

endwith

 


 

method: move

this method allows you to move items to new folders.

this example will use the find and findnext methods to find all message sent by ‘dan wilson’ and uses the move method to move all e-mail messages that where sent by ‘dan’ to a new folder.

 

example:

local loout   as outlook.application

local lons    as outlook.namespace

local lofold  as outlook.mapifolder

local loitems as outlook.items

local loitem  as outlook.mailitem

local lonew   as outlook.mapifolder

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

lofold = lons.getdefaultfolder(6) && inbox

*-- create a new folder for this example

lonew  = lofold.folders.add("dan wilson mail",6)       && inbox type.

*-- if folder already existed we would use the following code

*-- lonew = lofold.folders("dan wilson mail")

loitems = lofold.items

loitem  = loitems.find("[sendername] = 'dan wilson'")

do while vartype(loitem) = 'o' and !isnull(loitem)

       =loitem.move(lonew)

       loitem = loitems.findnext

enddo

 

method: printout

this method prints the outlook item using the default settings.

 

example:

local loout   as outlook.application

local lons    as outlook.namespace

local lofold  as outlook.mapifolder

local loitem  as outlook.mailitem

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

lofold = lons.getdefaultfolder(6) && inbox

loitem = lofold.items(1)                 && get first mail item

=loitem.printout

 

method: reply

this method creates a reply, pre-address to the original sender, from the original message, the object returned is a mailitem object.

 

example:

local loout   as outlook.application

local lons    as outlook.namespace

local lofold  as outlook.mapifolder

local loitem  as outlook.mailitem

local lorepl  as outlook.mailitem

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

lofold = lons.getdefaultfolder(6) && inbox

loitem = lofold.items(1)          && get first mail item

lorepl = loitem.reply                    && security dialog appears.

 

lorepl.body = "replying to this " + chr(13)+chr(10) + lorepl.body

lorepl.display

 


 

method: replyall

this method creates a reply to all original recipients from the original message, returns the reply as a mailitem object.

 

example:

local loout   as outlook.application

local lons    as outlook.namespace

local lofold  as outlook.mapifolder

local loitem  as outlook.mailitem

local lorepl  as outlook.mailitem

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

lofold = lons.getdefaultfolder(6) && inbox

loitem = lofold.items(1)          && get first mail item

lorepl = loitem.replyall          && security dialog appears.

 

lorepl.body = "replying to this " + chr(13)+chr(10) + lorepl.body

lorepl.display

 

 

method: save

this method saves the outlook item to the current folder or, if this is a new item, to the outlook default folder for the item type.

 

example:

local loout   as outlook.application

local lons    as outlook.namespace

local loitem  as outlook.mailitem

 

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

loitem = loout.createitem(0)      && new mail item

 

with loitem as outlook.mailitem

       .subject = "test for save - should go to drafts folder"

       .recipients.add("fred bloggs")    && security dialog.

       .save

endwith

 


 

method: saveas

this method saves the outlook item to the specified path and in the format of the specified file type, if file type is not specified then the msg format (.msg) is used.

parameters for this method are: path, type

where type: is one of the olsaveastype constants.

constant

value

description

oltxt

0

text format

olrtf

1

rich text format - need word as mail editor

oltemplate

2

outlook template

olmsg

3

msg format – default

oldoc

4

word format - need word as mail editor

olhtml

5

html format

olvcard

6

vcard format – contact items not mailitems

olvcal

7

vcal format – calendar items

olical

8

ical format – calendar items

olmsgunicode

9

msg unicode format.

taken from outlook help file

remarks

when you use the saveas method to save items to the file system, you receive an “address book” warning message. this includes all types of items, whether or not the items have attachments or active content. this change has been made so that someone cannot programmatically save items to a file and then parse the file to retrieve e-mail address. also note that even though ‘oldoc’ and ‘olrtf’ is a valid olsaveastype constants, messages in html format cannot be saved in document format, and the oldoc, olrtf constants work only if microsoft word is setup as the default email editor.

 

example:

local loout    as outlook.application

local lons     as outlook.namespace

local lofold   as outlook.mapifolder

local loitem   as outlook.mailitem

local lcname   as string

local lcprompt as string

 

#define oltxt 0

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

lofold = lons.getdefaultfolder(6) && inbox

*-- work through the inbox and save items

for each loitem in lofold.items

       && you may need to remove invalid filename characters

       && from the subject before saving to file.

       lcname   = loitem.subject

       lcprompt = "are you sure you want to save the item? " + ;

                  "if a file with the same name already exists, " + ;

                  "it will be overwritten with this copy of the file."     

       if messagebox(lcprompt,36+256,lcname) = 6

              =loitem.saveas("c:\temp\" + lcname + ".txt",oltxt)

       endif

endfor

 


 

method: send

this method sends the appointment, meeting item, e-mail message or task item.

 

example:

local loout    as outlook.application

local lons     as outlook.namespace

local loitem   as outlook.mailitem

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

loitem = loout.createitem(0)      && mail item

loitem.subject = "test for send"

loitem.recipients.add("fred bloggs")     && first security dialog

loitem.recipients.resolveall

loitem.body = "this is a test email"

loitem.send          && another security dialog - need to wait for timer.

 

 

method: showcategoriesdialog

this method displays the show categories dialog box, which allows you to select categories that correspond to the subject of the item.

 

exmaple:

local loout    as outlook.application

local lons     as outlook.namespace

local loitem   as outlook.mailitem

 

loout  = createobject('outlook.application')

lons   = loout.getnamespace("mapi")

loitem = loout.createitem(0)      && mail item

loitem.subject = "test for categories dialog."

loitem.recipients.add("fred bloggs")     && security dialog

loitem.display

loitem.showcategoriesdialog

 

Leave a Reply

Your email address will not be published. Required fields are marked *