Problem automating outlook with python

Noah noah at noah.org
Sat Aug 24 00:15:05 EDT 2002


xWestler <NoThanks@^*^#*&%.com> wrote in message news:<MPG.17d038f36d7b3105989680 at news.newsguy.com>...
> I'm brand new to python and I thought I'd try to duplicate a VBScript 
> I've been using to email documents using outlook.
> 
> This code produces a COM exception at the point the attachment is added.
> 
> here's the code:
> 
> import win32com.client
> outlook = win32com.client.Dispatch("Outlook.Application")
> message = outlook.CreateItem(0)
> message.Recipients.Add("me at example.com")
> message.Subject = "Message from Python"
> message.Body = "Message Body Text"
> message.Attachments.add("c:\Test.txt")
> message.Send()
> 
> Here's the exception:
> 
> com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft 
> Outlook', 'Cannot add the attachment; no data source was provided.', 
> None, 0, -2147352567), None)

The problem is that you are using a lowercase "add". You want "Add".
The attachment line should read:
    message.Attachments.Add(r"c:\Test.txt")
I don't know what the "add" method is, but apparently it is something -- 
just not the same thing as "Add" :-) I could not find "add" documented.
Probably it is inherited from somewhere.

Also, you want the raw style string r"...", otherwise the \ in c:\
will get treated as an escape character and might confuse things.
It's always good to use raw strings when working with Windows style path names.

Yours,
Noah



More information about the Python-list mailing list