0

How do you bring an application up as the top most window. I have two excel files opened up in excel. I close the first file, then reopen it up. But I want the second file to be as the top most window. How do you accomplish that without closing the second file and reopening it. I need to do this in dispatch in win32com only.

import time, os.path, os
from win32com.client import Dispatch

path1 = 'C:\\Todolist.xls'
path2 = 'C:\\Todolist2.xlsx'

xla = Dispatch("Excel.Application")
xla.DisplayAlerts = False
xla.Visible = True

xl = Dispatch("Excel.Application")
xl.DisplayAlerts = False
xl.Visible = True

wb1= xla.Workbooks.Open(Filename=path1)
wb2= xl.Workbooks.Open(Filename=path2)

wb1.Close()
time.sleep(3) 
wb1= xl.Workbooks.Open(Filename=path1)
#Need to bring wb2 back as the top most window
superx
  • 157
  • 13

1 Answers1

0

Try this

import win32gui

def windowEnumerationHandler(hwnd, windows):
    windows.append((hwnd, win32gui.GetWindowText(hwnd)))

windows = []
win32gui.EnumWindows(windowEnumerationHandler, windows)
for i in windows:
    if "window_heading" in i[1].lower():
        win32gui.ShowWindow(i[0],5)
        win32gui.SetForegroundWindow(i[0])
        break
Chintan Patel
  • 310
  • 1
  • 2
  • 11