Problem automating outlook with python 
Author Message
 Problem automating outlook with python

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.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)

Without that line the email is sent successfully (you have to use a real
address)

Any ideas or better sample code I can use?

This is under Windows XP Pro, Outlook 98, Python 2.2 (from ActiveState)
and PythonWin.

If you can provide some insight, thanks!



Wed, 09 Feb 2005 01:21:34 GMT  
 Problem automating outlook with python
Maybe I am missing something obvious, but don't you have to use

"c:\\test.txt"

instead of

"c:\test.txt"?

or use os.path.join("c:", "test.txt")

Quote:

> 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.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)

> Without that line the email is sent successfully (you have to use a real
> address)

> Any ideas or better sample code I can use?

> This is under Windows XP Pro, Outlook 98, Python 2.2 (from ActiveState)
> and PythonWin.

> If you can provide some insight, thanks!
> --
> http://mail.python.org/mailman/listinfo/python-list

--
Anthony Tuininga

Computronix
Distinctive Software. Real People.
Suite 200, 10216 - 124 Street NW
Edmonton, AB, Canada  T5N 4A3
Phone:  (780) 454-3700
Fax:    (780) 454-3838
http://www.computronix.com



Wed, 09 Feb 2005 02:11:51 GMT  
 Problem automating outlook with python


Quote:
> Maybe I am missing something obvious, but don't you have to use

> "c:\\test.txt"

> instead of

> "c:\test.txt"?

> or use os.path.join("c:", "test.txt")

The double backslash was a good idea.  I'm embarrassed that I didn't
think to try it.

Unfortunately neither the double backslash or the os.path.join() made
any difference.  Exact same COM exception.

As a sanity check I added code to open the file and read it.  After
playing with various combinations of various slashes, what seemed to
work for reading the file was "c:/text.txt".  In the code below:

f = open("c:/test.txt", "r")
print f.read()
f.close()
message.Attachments.add("c:/test.txt")

I see the file contents printed out but the attachment statement still
throws the same exception.

Thanks for your help though.

Quote:

> > 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.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)

> > Without that line the email is sent successfully (you have to use a real
> > address)

> > Any ideas or better sample code I can use?

> > This is under Windows XP Pro, Outlook 98, Python 2.2 (from ActiveState)
> > and PythonWin.

> > If you can provide some insight, thanks!
> > --
> > http://mail.python.org/mailman/listinfo/python-list



Wed, 09 Feb 2005 03:13:49 GMT  
 Problem automating outlook with python

Quote:

> 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.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



Wed, 09 Feb 2005 12:15:05 GMT  
 Problem automating outlook with python


Quote:
> 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

Thank you very much Noah.  That did it.


Fri, 11 Feb 2005 22:55:37 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. automating Outlook [repost]

2. automate retrieving/sending outlook email via tcl/expect

3. automating Outlook [repost]

4. PythonCOM Problem: Automating WinFAX from Python

5. Automating Word from WinNT Python Service problem

6. import contact list from Outlook or Outlook Express

7. How do I trigger a send/receive in Outlook or Outlook Express

8. Outlook/Outlook Express replacement

9. Outlook 2002 and Python

10. Using python to add contacts to outlook express 6

11. Backing up mail in Outlook Express using Python

12. get information from MS Outlook using Python

 

 
Powered by phpBB® Forum Software