This is my code so far:
import win32com.client
o = win32com.client.gencache.EnsureDispatch("Outlook.Application")
ns = o.GetNamespace("MAPI")
adrLi = ns.AddressLists.Item("Global Address List")
contacts = adrLi.AddressEntries
numEntries = adrLi.AddressEntries.Count
nameAliasDict = {}
for i in contacts:
name = i.Name
alias = i.Address.split("=")[-1]
print i.GetExchangeUser().PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3A56101E")
I"m getting the property from: https://msdn.microsoft.com/en-us/library/bb446002.aspx
But for some reasons, I get this error:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u'The property "http://schemas.microsoft.com/mapi/proptag/0x3A550003" is unknown or cannot be found.', None, 0, -2147221233), None)
Am I doing this wrong?
You cannot assume that PR_CONTACT_EMAIL_ADDRESSES or any other MAPI property will be available. Your code must expect and handle that error from PropertyAccessor.GetProperty.
Check if you can actually see the property on that particular object in OutlookSpy (I am its author - click IAddrBook, "Open Root Container" etc.).
Why exactly do you need that property? If you just need the SMTP address. use ExchangeUser.PrimarySmtpAddress.
Related
I am currently trying to convert some word documents to PDF using python, some of the files converted successfully with the code I have while I got the following error messages on other files:
Error 1: some files reported
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'String is longer than 255 characters', 'wdmain11.chm', 41873, -2146819183), None)
Error 2: Some reported
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'Command failed', 'wdmain11.chm', 36966, -2146824090), None)
Error 3: Some reported
AttributeError: 'NoneType' object has no attribute 'SaveAs'
Below is the code I am using, any idea why this is happening:
from docx2pdf import convert
def convert_word_to_pdf(word_path):
pdf_path = word_path.replace(".docx", ".pdf")
convert(word_path, pdf_path)
word_path = '*file_path*'
convert_word_to_pdf(word_path)
Thank you for your support
I expected the python code above to convert all the files to PDF.
I'm using python to save emails in a specified folder and complete the following:
check if the email is unread
save the email to target folder
mark the message as read
tell me how many messages were archived and the location
the code:
from win32com.client import Dispatch
import os
outlook = Dispatch('outlook.application').GetNamespace("MAPI")
root = outlook.Folders[0].Folders[27]
targetPath = os.path.expanduser(r'\\path\to\target\folder')
messages = root.Items
count = 0
for msg in messages:
if msg.UnRead:
msg.SaveAs(os.path.join(targetPath))
msg.UnRead = False
count +=1
print(str(count)+' emails archived to: '+targetPath)
I end up with this error i've never seen before, and I haven't had any luck with the google machine... so I come to stackoverflow and wake you from your slumber.
Traceback (most recent call last):
File "C:\Users\e582324\Documents\Python Scripts\untitled6.py", line 18, in <module>
msg.SaveAs(os.path.join(targetPath))
File "<COMObject <unknown>>", line 3, in SaveAs
com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The operation failed.', None, 0, -2147024829), None)
Any idea whats causing this? I have a similar script that saves just attachments and it works fine. My target folder is on a network shared drive for both scripts and it hasn't caused an issue until now. Only real difference is the use of .SaveAs() instead of .SaveAsFile()
You need to specify the file name as well as its extension. passing the path is not enough sometimes, especially when you don't specify the format (see the second parameter). Note, an Outlook folder may contain different kind of items - mail items, appointments, documents, notes and etc. For example, here is a sample:
Item.SaveAs Environ("HOMEPATH") & "\My Documents\" & strname & ".txt", olTXT
Also I've noticed that you are iterating over all items in the folder:
for msg in messages:
if msg.UnRead:
Instead, you need to use the Find/FindNext or Restrict methods of the Items class. So, you get only items that correspond to your conditions and iterate only over them. Read more about these methods in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
I had a code that ran well for 3 months up until now I have started seeing errors and not sure what needs revision
My original code was this:
import pandas as pd
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")
root_folder = namespace.Folders.Item(3)
subfolder = root_folder.Folders['Inbox'].Folders['Daily Process']
messages = subfolder.Items
message = messages.GetFirst()
subj_line = message.subject
However I am getting the following error
com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The attempted operation failed. An object could not be found.', None, 0, -2147221233), None)
I broke my code down and seems like the name 'MAPI' is not longer defined
import pandas as pd
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")
your_folder = namespace.Folders['Inbox'].Folders['Daily Process']
for message in your_folder.Items:
print(message.Subject)
Which gives me
name 'mapi' is not defined
Try in lower case:
outlook = win32com.client.Dispatch("outlook.application")
I also had the same error when I put capitalized Outlook
I tried the following script but that is giving me the error
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2146959355, 'Server execution failed', None, None)
objAccess = win32com.client.Dispatch("Access.application")
objAccess.compactRepair(srcDB, destDB)
Can anybody help me with this please.
To open the database in exclusive mode, you need to change "/" to "backslash" \ in the path:
path_bdd_access = path_bdd_access.replace('/', chr(92))
I am using PIOLEDBENT provider, i need to extract list of databases in my PIAF Server when i call oConn.GetSchema i throws exception mention bellow. please guide me what is wrong with my code.
from win32com.client import Dispatch
oConn = Dispatch('ADODB.Connection')
oRS = Dispatch('ADODB.RecordSet')
oConn.ConnectionString = "Provider=PIOLEDBENT; Data Source=localhost; Integrated Security=True; Integrated Security=True;User ID=abubakr;Password=#Spark123!"
oConn.Open()
oConn.GetSchema();
Exception :
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'ADODB.Connection', u'Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.', u'C:\\Windows\\HELP\\ADO270.CHM', 1240641, -2146825287), None)