0

I have a sheet that contains a macro (.xlsm). When this macro is ran, it requires to select another .csv file and this creates an .xlsx file. So far, I managed to open the .xlsm file and run the macro, but I do not know how to make it choose a specific .csv file and how to save that results.

My code is:

import win32com.client
import os

directory = "C:/Users/aprofir/Desktop/"
file = "CSVDE OPEN.xlsm"
macro = "Csvde"
path = os.path.join(directory, file)

if os.path.exists(path):
    xlApp = win32com.client.Dispatch("Excel.Application")
    wb = xlApp.Workbooks.Open(Filename=path, ReadOnly=1)
    xlApp.Application.Run(macro)
    xlApp.Application.Run()
    wb.Close(SaveChanges=1)
    xlApp.Application.Quit()

If I run this code, the macro wants me to choose a file: enter image description here

How do I make it select the file at a specific location?

Adrian
  • 774
  • 7
  • 26
  • 1
    Is this file-location a parameter to the macro? If so, pass this parameter when you invoke the macro using `Application.run`. On the other hand, if the macro initializes a dialogue with the user, it is all of a sudden a much harder problem whose simplest solution would involve changing the macro. In general, code which is designed to require user-input (as opposed to passed parameters) is much, much harder to invoke programmatically. – John Coleman Mar 13 '19 at 12:18
  • @JohnColeman It is a dialogue with the user. I think I will try to change the macro to make the location of the file a parameter. Thank you for the input! – Adrian Mar 13 '19 at 12:30
  • You may pass the path via process environment variable, check [this answer](https://stackoverflow.com/a/34500601/2165759). – omegastripes Mar 13 '19 at 14:25

0 Answers0