Home > Enterprise >  win32com Outlook client has no attribute 'GetNameSpace'
win32com Outlook client has no attribute 'GetNameSpace'

Time:01-24

I was working well with GetNameSpace('MAPI'), but I wrote an ActiveState code: import Outlook contacts using win32com and GetNameSpace is not longer useful.

Now the code has changed: Is useful GetNamespace('MAPI'). Why did this happen and how to return to GetNameSpace('MAPI')?

import win32com.client as client
outlook = client.Dispatch('Outlook.Application')
namespace = outlook.GetNameSpace('MAPI')
drafts = namespace.GetDefaultFolder(16)
contacts = namespace.GetDefaultFolder(10)
inbox = namespace.GetDefaultFolder(6)

Result:

AttributeError: '<win32com.gen_py.Microsoft Outlook 16.0 Object Library._Application
  instance at 0x2538077068688>' object has no attribute 'GetNameSpace'

I am working on VSCode.

CodePudding user response:

Nothing has been changed so far in the Outlook object model. The Session property and the GetNamespace method can be used interchangeably to obtain the NameSpace object for the current session. Both members serve the same purpose. For example, the following statements do the same function:

Set objNamespace = Application.GetNamespace("MAPI") 

Set objSession = Application.Session

Note, sometimes, when you have got several profiles configured in Outlook, you may need to use the Logon method of the GetNamespace method.

CodePudding user response:

At the basic level, the issue is a typo on GetNameSpace which should have a lower-case 's'.

Here is the MS documentation

So this code works:

import win32com.client as wc

ol = wc.gencache.EnsureDispatch('Outlook.Application')
namespace = ol.GetNamespace('MAPI')

But this does not:

import win32com.client as wc

ol = wc.gencache.EnsureDispatch('Outlook.Application')
namespace = ol.GetNameSpace('MAPI')

At a deeper level, this is less trivial, because in other circumstances you might get away with either case:

This code works:

import win32com.client as wc

ol = wc.dynamic.Dispatch('Outlook.Application')
namespace = ol.GetNameSpace('MAPI')

Even Microsoft themselves switch between 's' and 'S' in their documentation. The difference comes from early vs late binding of the COM object.

If you have previously called win32com.client.gencache('Outlook.Application') as in the ActiveState example the OP references, then win32com will/may have created an early-binding wrapper from the Outlook type library. This wrapper is case-sensitive and persists so that it is available for other applications. When you call win32com.Dispatch('Outlook.Application') the win32com package will try and use this wrapper if it is available. If you were using VBA this is akin to including Outlook as a Reference for your code, and using Dim ol As Outlook.Application.

If however you had not called gencache earlier, win32com.Dispatch('Outlook.Application') would be using late-binding and the IDispatch interface (akin to calling win32com.dynamic.Dispatch()), where methods and property names are looked up dynamically at run time, via the low-level IDispatch::GetIDsOfNames() call. This lookup is case-insensitive (see the comment in the docs), so doesn't differentiate between 's' and 'S'. This is also true in VBA if you had used Dim ol as Object, with no Reference to the Outlook type library.

  •  Tags:  
  • Related