Reputation: 1
I am new to python. I am trying to connect my python code with the open (or active?) instance of excel via
import win32com.client as win32cl
excel=win32cl.GetActiveObject('Excel.Application')
It retrieves
Exception has occurred: com_error
(-2147221021, 'Operation unavailable', None, None)
while Excel is open as I already started it as regular app in windows.
Moreover, I can run this successfully:
import win32com.client as win32cl
excel=win32cl.Dispatch('Excel.Application')
and I have a new instance of Excel running.
after this .GetActiveObject
works fine with this new instance, but not with the manually opened Excel.
The same issue also exists with Access.
any idea how to resolve?
Upvotes: 0
Views: 1931
Reputation: 49455
Most probably your existing Excel Application
is run under a different security context in Windows. Make sure that both applications (your python script and Excel) are run with the same privilege level, otherwise you will not be able to get the running instance. For example, when your code is run with administrative privileges you need to run Excel using the Run As Admin
command to get them both under the same security context.
To put it simply, a security context is a bit of cached data about a user, including her SID, group SIDs, privileges, and some other stuff. One of the fundamental tenets of Windows security is that each process runs on behalf of a user, so each process has a security context associated with it, like a global variable controlled by the kernel. This allows the system to audit the actions taken by a process and make access control decisions when the process acquires resources.
Upvotes: 0