0

I'm writing a Python script to automate a process that uses Excel macros to format files to input into a different program later on. I'm a bit new to Python and completely new to VBA, but I think I have the steps down.

So far I have this to run the macro itself (with help from other SO posts):

xl = win32com.client.DispatchEx('Excel.Application')
xlpath = os.path.expanduser(xlfile)
wb = xl.Workbooks.Open(Filename=xlpath, ReadOnly=1)
xl.Run("my_macro")

At this point the macro runs and calls Application.GetOpenFilename() to open a dialog for the user to choose the file to be formatted, which is going to be different for each macro.

Basically my user is going to have different initial data to format each time they run my script. At the beginning I want them to choose the files that they need formatted and then I'll save those file paths. Then I want to plug those file paths into the macro from Python instead of opening the dialog in Excel.

Is there a way to do this directly by changing the macro? If not, will I need to rewrite the macro in Python with one of the modules out there for driving Excel?

1 Answer 1

0

It looks like I can call xl.Run("my_macro") with parameters so I just have to figure out how the macro reads in those parameters so once I pass in the paths it can just open the files without the dialog.

xl.Run("my_macro", path1, path2)

https://stackoverflow.com/a/16740500/3788802

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.