Search code examples
pythonpython-3.xpywin32

How to Get pywin32 Methods to Accept Variable Parameters


I'm using pywin32 to write a program that sends email notifications. Based on error exceptions

def main(recipient):
try:    
    mailer = Dispatch("Outlook.Application")
    msg = mailer.CreateItem(0)
    msg.To = recipient
    msg.CC = ""
    msg.Subject = "EmailTest"
    msg.Body = "This is most certainly a test"
    msg.Send()

    success(msg.Subject, recipient)
except Exception:
    failure(msg.Subject, recipient)

When the parameters to the success and fail methods are variables (with string values) it returns the error:

Traceback (most recent call last):
  File "C:\pathtoprogram\thisprogram.py", line 48, in main
    success(msg.Subject, recipient)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python36-32\lib\site-packages\win32com\client\dynamic.py", line 516, in __getattr__
    ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The item has been moved or deleted.', None, 0, -2147221238), None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\pathtoprogram\thisprogram.py", line 50, in main
    failure(msg.Subject, recipient)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python36-32\lib\site-packages\win32com\client\dynamic.py", line 516, in __getattr__
    ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The item has been moved or deleted.', None, 0, -2147221238), None)

I don't know what's creating the exception, but the code works fine when I enter the parameters directly as strings.

success("mySubject", "anEmail")
except Exception:
    failure("mySubject", "anEmail")

That code works will run fine, but I need the parameters to be variables because they need to be able to be passed in from the main method.

I've been looking up answers to similar problems but I haven't found one that pertains to this issue. It seems like msg.Subject and msg.To don't actually store string values even though they're in what looks like any other assignment statement. Maybe it's an obvious thing I'm missing? Any help would be greatly appreciated.

I'm using python 3.6 and the corresponding version of pywin32.


Solution

  • The problem is not with the way you are using the variables. You can test this by checking the values of msg.Subject before you call msg.Send()

    Refer to the CDO (collaboration data objects) documentation from Microsoft, specifically for the Send method:

    https://msdn.microsoft.com/en-us/library/ms527190(v=exchg.10).aspx

    Send moves the message to the current users Outbox folder. Messaging systems retrieve messages from the Outbox and transport them to the recipients. After it is transported, a message is removed from the Outbox and deleted unless saveCopy is True.*

    Refer to the error message you were getting:

    4096, 'Microsoft Outlook', 'The item has been moved or deleted.'

    So the system is doing what it was designed to do - delete the message after you send it.