0

Is it possible to import and run VBA functions in Python?

For example, let mymodule.bas include the following function:

Function AddTwoNumbers(ByVal Alpha As Variant, ByVal Beta As Variant) As Variant
  AddTwoNumbers = (Alpha + Beta)
End Function

Is there an extension I can use to interface to such function using Python? I was thinking of using pywin32 but it is not clear to me whether this only works with vba functions inside a MS Excel spreadsheet.

7
  • 1
    What is a "standalone VBA file"? That doesn't exist. VBA is always a part of an Excel, Word or other MS Office file (it cannot be stand alone). Do you mean VBScript instead? But this is a completely different language! Be very clear about the terms you use. Also please be more specific what you mean with "import the module"? You want to translate it into python or do you want to call a function? Your question is very unclear. Commented Nov 26, 2018 at 12:25
  • 1
    @Pᴇʜ, I believe OP is talking specifically about the exported VBA file which is a .bas file. So: "Yes, a standalone VBA file" which is useless unless imported into an office application's VBE. Commented Nov 26, 2018 at 13:31
  • 1
    OP, that being said, this doesn't make a lick of sense. VBA only works inside of an MS Office environment. You can't just steal the VBA from your VBE (Even exporting it) and expect it to run anywhere. It won't. It will only run inside that environment. Use a library for Python that is meant to work with the Office application you are wanting to automate. Commented Nov 26, 2018 at 13:32
  • For instance, this question has a brief write up on how to use win32 extension in python. You will have to rewrite your VBA to work in Python though, there will be no copy/paste way of doing this. Commented Nov 26, 2018 at 13:36
  • How do you run your .bas file from commandline? Commented Nov 26, 2018 at 17:01

1 Answer 1

2

The answer to your question is:

  • It is not possible to import a mymodule.bas (that was exported from VBA) into Python.
  • It is not possible to run a VBA code (mymodule.bas) that was exported from VBA stand alone.
  • There is no way to automatically translate VBA into Python.

Possibilities you have:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.