0

I am trying to run vba macro from Python but the excel file which has macro is already open. So I want to not open it again as another instance.

I have tried win32com code - however it reopens another instance of the same file and makes changes in that instance and ask me to save as second excel file. But i want to call macro in already open file. and after calling that macro leave the excel file open. this is the path where File is located - its a private shared drive

\nplofnp0001a\gtonnnfs10912400\CPM Other\PL Estimate\PL Estimate Sheets\DAILY PL ESTIMATE W NRPD RV2 v6_YK_v2.xlsm

folder_xlfile = '\\\\nplofnp0001a\gtonnnfs10912400\CPM Other\PL Estimate\PL Estimate Sheets'
xlfile = '\\DAILY PL ESTIMATE W NRPD RV2 v6_YK_v2.xlsm'

Filename=folder_xlfile + xlfile

import win32com.client

xl=win32com.client.DispatchEx("Excel.Application")
#wb = xl.Workbooks.Open(Filename)
xl.Application.Run(Filename + "!Module2.CopySummaryData")
#xl.Application.Quit()
del xl

Expected Result - run vba function in already open excelfile.

Actual Result - just keeps running without error message. I guess code hangs.

braX
  • 11,506
  • 5
  • 20
  • 33
  • Have you tried something like `xl = win32com.client.GetObject("Name_Of_Workbook") ` This should let you get the open instance of the workbook in question. https://stackoverflow.com/questions/2876755/get-name-of-active-excel-workbook-from-python – Mike Sep 18 '19 at 19:00

1 Answers1

-1
xl.Application.Run(r"'DAILY PL ESTIMATE W NRPD RV2 v6_YK_v2.xlsm'!Module2.CopySummaryData")
Hadi GhahremanNezhad
  • 2,377
  • 5
  • 29
  • 58
qwcbn
  • 1