0
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 
outlook2 = win32com.client.Dispatch("Outlook.Application") 
ns = outlook2.GetNamespace("MAPI") 
ns.Logon("Outlook2010") 
mail = outlook2.CreateItem(0) 
inbox = outlook.GetDefaultFolder(6) 
messages = inbox.Items
for i in processed_corpus:
  for j in i:
     if j == "pricing" or j == "price" or j == "quote" or j =="quotation":
        mail.To = "abc@xyz.com"
        mail.Subject = message.Subject
        mail.Body = message.Body + "Belongs To Pricing"
        mail.Send()
        # print("Belongs to Pricing")
        # print("**************")
        # print(j)
        worksheet.write(row,col +4,"Pricing")
        ram=1
     elif j == "service" or j=="request" or j=="servicerequest":
        mail.To = "abc@xyz.com"
        mail.Subject = message.Subject
        mail.Body = message.Body + "Belongs To Service Request"
        mail.Send()
        # print("Belongs to servicerequest")
        # print("**************")
        # print(text_corpus)
        # print(j)
        worksheet.write(row, col + 4, "Service Request")
        ram=1
if(ram==0):
    #print("nothing")
    worksheet.write(row, col + 4, "Category Not Defined")
     # else:
     #    print("Belongs to nothing")
     #    print("**************")
     #    worksheet.write(row, col + 4, "Category Not Defined")

row = row + 1
message = lastHourMessages.GetNext()
wb.save("C:\\Users\\Innovation\\Desktop\\Demo_Format.xls")`

This is the error I'm getting:

Traceback (most recent call last):

File "28/11/2k19.py", line 81, in <module>
     mail.To = "abc@xyz.com"
File "venv\lib\site-packages\win32com\client\dynamic.py", line 549, in __setattr__
     self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value) 
     pywintypes.com_error: (-2147352567, 'Exception occurred.', 
         (4096, 'Microsoft Outlook', 'The item has been moved or deleted.',
                None, 0, -2147221238),
     None)
trincot
  • 317,000
  • 35
  • 244
  • 286
dartKnightRises
  • 815
  • 15
  • 26
  • Is the indentation of last 10 lines correct -- is it outside of the loops that you start at the top? Secondly, where did you initialise the `mail` variable? You should create a new instance in every iteration of the inner loop... – trincot Dec 02 '19 at 07:03
  • Hi @trincot Everything is working fine. There is no indentation problem, those loops and according to my project requirement. Whenever I fetch single mail from outlook and send it, then it works fine. Error only appears whenever I try to send more than one mail. – dartKnightRises Dec 02 '19 at 07:07
  • And my second question about `mail`? And obviously everything is not working fine. I just want to know why you add the part after the loop, when the error is in the loop... That's why I also asked about indentation. If it is not in the loop, please remove the part of the code that is never executed. It cannot be relevant for your error/question then. – trincot Dec 02 '19 at 07:10
  • I don't know python, but what are you trying to do? It looks like you are writing to a Excel sheet with what your mails are? You can do that in Outlook also with VBA and have it run automatically when a email arrives. – Andreas Dec 02 '19 at 07:22
  • Hi @trincot, I've defined the mail outside the loop and performing text processing on the mail body, if mail body contain those specific words it will categories them accordingly and the save the details into workbook and also send mail body to specific person through mail. – dartKnightRises Dec 02 '19 at 09:51
  • Hi @Andreas, I'm performing text processing on the received mail body and then categories them accordingly and then send mail body to specific person according to their category. Also saving them into workbook. – dartKnightRises Dec 02 '19 at 09:52
  • *"defined the mail outside the loop"*: that is the problem. You need a new email object each time you want to edit and send one. Could you please *add* that `mail`-definition code to the question? – trincot Dec 02 '19 at 09:54
  • outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") outlook2 = win32com.client.Dispatch("Outlook.Application") ns = outlook2.GetNamespace("MAPI") ns.Logon("Outlook2010") mail = outlook2.CreateItem(0) inbox = outlook.GetDefaultFolder(6) messages = inbox.Items – dartKnightRises Dec 02 '19 at 09:57
  • Ok @trincot, now I am going to define mail inside the loop. – dartKnightRises Dec 02 '19 at 09:59
  • Note that the indentation is probably wrong. You'll want the `if(ram==0)` part to be *inside* the loops, and probably some of the following statements as well. Please correct this in your question. See my very first comment at the top. – trincot Dec 02 '19 at 10:10

1 Answers1

0

The problem is that you are only creating one mail object with CreateItem(0), and once you have sent it, you are not allowed to modify it any longer in the next iteration of your loop.

What you really want is to create a new mail in each iteration. So change this:

mail = outlook2.CreateItem(0) # <-- this should be moved inside the inner loop
inbox = outlook.GetDefaultFolder(6) 
messages = inbox.Items
for i in processed_corpus:
  for j in i:
    if j == "pricing" or j == "price" or j == "quote" or j =="quotation":

to this:

inbox = outlook.GetDefaultFolder(6) 
messages = inbox.Items
for i in processed_corpus:
  for j in i:
    mail = outlook2.CreateItem(0) # <--- moved here
    if j == "pricing" or j == "price" or j == "quote" or j =="quotation":
trincot
  • 317,000
  • 35
  • 244
  • 286