1

I have a VBA toolbar that i have been working on. It has two buttons, and near the end of the process, it calls a python script. What I want to do is depending on which of the two buttons is clicked, a certain part of the python script will run, so I want to pass a value that is linked to the button which is then sent to the python script and run.

How do I do this?

Thanks

2
  • How do you run the Python script? Via a new process with a command line? Commented May 31, 2012 at 10:27
  • Martijn, within the VBA code i have the following codeShell "C:\Python25\python.exe ""H:\Report_v7.py" Commented May 31, 2012 at 10:29

1 Answer 1

1

You can pass command line options to the python script, just like you can with other command line programs. Depending on which button was pressed, pass different switches to your program.

In your case, it may be simplest just to pass in one value on the command line depending on the button that was pressed and pick this from the sys.argv variable:

import sys

def fooClicked():
    # Stuff to do when Foo was clicked

def barClicked():
    # Stuff to do when Bar was clicked

button = sys.argv[1]
if button == 'foo':
    fooClicked()
elif button == 'bar':
    barClicked()

(You could use a dict to look up methods but that may be too advanced, don't know how comfortable you are with Python).

So, if you call this script with python.exe H:\Report_v7.py foo the fooClicked function will be called.

If this is going to grow to more than just two buttons, I'd use the optparse module to define your options and run different code paths depending on the options chosen.

If you upgrade to Python 2.7, then use the new (better) argparse module instead.

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.