Search code examples
pythonpywin32win32com

From an Administrator Python window, create/attach COM objects as a user


I have to run python as an admin to access a WPF application with pywinauto. The WPF application exports data into an excel sheet that it saves to its cache directory on the hard drive and opens.

This fails in my admin python interpreter. It creates a new excel process even though I'm trying to connect to the one that's already open. It works fine in a regular non-admin python interpreter window:

import win32com
xlApp=Dispatch.GetActiveObject("Excel.Application")
xl.quit()

How Can I use win32com at a user level from an admin level python interpreter? Or how else can I solve this issue?

My current ugly solution is to use Pywinauto to target the excel window, save the title to a variable, kill the window and then use dispatch from the admin window to re-open the file and do my com operations. It's wasteful but it gets the job done.


Solution

  • Okay I figured out what was going on. During testing I had closed some windows of the WPF application and reopened them manually.

    When I opened the WPF with my admin python window, I was making admin WPF window that spawned admin level excel sheets.

    When I manually launched the WPF windows I was creating USER level WPF windows that spawned USER level excel sheets.

    Admin python can't COM the user level excel sheets

    User python can't COM the admin level excel sheets.

    So as long as I launch all of my WPF windows from my admin python session I can use COM the way I want to.