Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Outlook 2007 COM primer

I've been inspired by Modifying Microsoft Outlook contacts from Python -- I'm looking to try scripting some of my more annoying Outlook uses with the win32com package. I'm a Linux user trapped in a Windows users' cubicle, so I don't know much about COM.

I'm looking for information on whether COM allows for reflection via win32com or whether there's documentation on the Outlook 2007 COM objects. Any other pointers that you think will be helpful are welcome!

I've found Programming Outlook With Python, but I'm using Outlook 2007 so I'd like some more information on how much of the Outlook 2000 information is still applicable.

TIA!

like image 324
cdleary Avatar asked Jan 02 '09 21:01

cdleary


3 Answers

To answer your question about documentation. Here are two links that I regularly visit when developing Outlook macros. While the sites are primarily focused on development with MS technologies most of the code can be pretty easily translated to python once you understand how to use COM.

  • http://msdn.microsoft.com/en-us/library/bb176619.aspx]1
  • http://www.outlookcode.com/
  • Deal Outlook Security http://www.outlookcode.com/article.aspx?ID=52
  • Redemption http://www.dimastr.com/redemption/
like image 119
Zoredache Avatar answered Nov 04 '22 01:11

Zoredache


In general, older references to the object model are probably still valid given the attention Microsoft pays to backwards-compatability.

As for whether or not you will be able to use win32com in python for Outlook, yes, you should be able to use that to make late-bound calls to the Outlook object model. Here is a page that describes how to do it with Excel:

http://oreilly.com/catalog/pythonwin32/chapter/ch12.html

A problem that you should be made aware of is the fact that Outlook has a security dialog that is thrown up when external programs try to access the object model and perform operations in outlook. You are not going to be able to suppress this dialog.

If you want to avoid the dialog, you are better off creating macros in VBA for Outlook that are loaded in a session, and put buttons on a new CommandBar to execute them.

like image 36
casperOne Avatar answered Nov 04 '22 00:11

casperOne


This was my implementation from a couple years ago. I used it to automate the receiving and sending of email. Not sure if this will work with 2010. It depends on Redemption as well.

import win32com.client,os,re
from utils.autoencode import autoencode 
generated='2D5E2D34-BED5-4B9F-9793-A31E26E6806Ex0x4x7.py'
mapi_utils=win32com.client.Dispatch('Redemption.MAPIUtils')
olFolderDeletedItems=3
olFolderOutbox=4
olFolderSentItems=5
olFolderInbox=6
olFolderCalendar=9
olFolderContacts=10
olFolderJournal=11
olFolderNotes=12
olFolderTasks=13
class Attachment:
    def __init__(self,CreationTime,attachement):
    self.CreationTime=CreationTime
    self.attachement=attachement
    self.FileName=attachement.FileName
    self.FileSize=attachement.FileSize
    self.text=self.attachement.AsText

    def Save(self,folder,filename=None,group=True):
    if group:
        folderGroup=re.sub('\\W','',str(self.CreationTime))
        subfolder=os.path.join(folder,folderGroup)
        if not os.path.isdir(subfolder):
        os.mkdir(subfolder)
    else:
        folderGroup=''
    if filename:
        path=os.path.join(folder,folderGroup,filename)
    else:
        path=os.path.join(folder,folderGroup,self.FileName)
    if os.path.isdir(folder):
        self.attachement.SaveAsFile(path.replace('/','\\'))
        return path

class Attachments:
    def __init__(self,CreationTime,Attachments):
    self.CreationTime=CreationTime
    self.Attachments=Attachments
    def __iter__(self):
    return self.next()
    def next(self):
    for idx in range(self.Attachments.Count):
        idx+=1
        yield Attachment(self.CreationTime,self.Attachments.Item(idx))

class Message:
    def __init__(self,store,folder,msg):
    self.store=store
    self.folder=folder
    self.msg=msg
    self.Attachments=Attachments(self.msg.CreationTime,msg.Attachments)
    self.body=msg.Body
    self.body_format=msg.BodyFormat
    self.html=msg.HTMLBody
    self.subject=msg.Subject
    self.unread=msg.UnRead
    self.id=msg.EntryID
    def __str__(self):
    return str('%s-%s-%s'%(self.store.Name,self.folder, self.msg))
    def read(self,bool=True):
    status=bool==False
    self.msg.UnRead=status
class Inbox:
    def __init__(self,session,store,folder,wantedFolder=None):
    self.session=session
    self.store=store
    self.folder=folder
    self.wantedFolder=wantedFolder
    self.Name=folder.Name
    def __getitem__(self,name):
    self.wantedFolder=name
    return self.next()    
    def __str__(self):
    return '%s-%s'%(self.store.Name,self.Name)

    def __iter__(self):
    return self.next()
    def subFolder(self,name):
    self.wantedFolder=name
    return self.next()
    def next(self):
    if self.wantedFolder:
        subFolders=self.folder.Folders
        for idx in range(subFolders.Count):
        idx+=1
        subfolder=subFolders.Item(idx)
        if subfolder.Name==self.wantedFolder:
            for msg in subfolder.Items:
            yield Message(self.store,self.folder,msg)
    else:
        for msg in self.folder.Items:
        yield Message(self.store,self.folder,msg)



class Store:
    def __init__(self,session,store):
    self.session=session
    self.store=store
    self.Name=store.Name
    self.Inbox=Inbox(self.session,self.store,self.session.GetDefaultFolder(olFolderInbox))
    def __str__(self):
    return self.Name
    def __iter__(self):
    return self.next()
    def next(self,folder=None):
    pass 


class rdo:
    def __init__(self):
    '''Outlook Redemption RDO wrapper'''
    self.session = win32com.client.gencache.EnsureDispatch("Redemption.RDOSession")

    self.session.Logon()
    self.stores={}
    for store in self.session.Stores:
        self.stores[store.Name]=Store(self.session,store)
    self.default_store=self.session.Stores.DefaultStore.Name
    def __getitem__(self,name):
    if self.stores.has_key(name):
        return self.stores[name]
    def __iter__(self):
    return self.next()
    def next(self):
    stores=self.stores.keys()
    yield self.stores[stores.pop(stores.index(self.default_store))]
    for store in stores:
        yield self.stores[store]
    def getStore(self,name):
    if self.stores.has_key(name):
        return self.stores[name]
    else:
        return False
    def getSharedMailbox(self,name):
    try:
        return Store(self.session,self.session.GetSharedMailbox(name))
    except Exception,e:
        if 'Could not resolve in GAL' in  e.args[2][2]:
        raise Exception('Mailbox could not be found')
        else:
        raise Exception ('Unknown error: %s'%e.args[2][2])

if __name__=='__main__':
    r=rdo()
    inbox = r.getStore('Mailbox - Foo').Inbox
    for msg in inbox.subFolder('test'):
    print msg.subject,msg.id
like image 1
ebt Avatar answered Nov 04 '22 01:11

ebt