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.

braX
  • 11,506
  • 5
  • 20
  • 33
Kievit
  • 87
  • 10
  • 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. – Pᴇʜ Nov 26 '18 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. – JNevill Nov 26 '18 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. – JNevill Nov 26 '18 at 13:32
  • For instance, [this question has a brief write up on how to use `win32 extension`](https://stackoverflow.com/questions/15467229/automation-excel-from-python) in python. You will have to rewrite your VBA to work in Python though, there will be no copy/paste way of doing this. – JNevill Nov 26 '18 at 13:36
  • How do you run your *.bas* file from commandline? – CristiFati Nov 26 '18 at 17:01
  • @CristiFati I'm pretty sure you cannot run a `.bas` file (VBA) from the command line. VBA needs to run in an application like Microsoft Office (therefore it needs to be imported in Office before you can run it.) – Pᴇʜ Nov 27 '18 at 07:01
  • @Pᴇʜ: Thank you for the clarification. I thought the code is the same (and can be run via *cscript*), but it's not the case. – CristiFati Nov 27 '18 at 10:43

1 Answers1

1

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:

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73