I want to extract the VBA
code from an .xlsm
file and save it as .txt
files. Unfortunately, I get only .bin
files when I unzip the .xlsm
file which I cannot open with an editor.
Asked
Active
Viewed 73 times
-1
-
That's how Excel stores the code. If you really want to read the file, have a look to https://stackoverflow.com/questions/17536607/read-vba-macros-or-vbaproject-bin-of-an-excel-file-without-opening-it-in-ms-ex. Sounds like fun. Not. Any reason you don't want to do this with VBA (access the VBE project object modell, tons of examples around) – FunThomas Jun 15 '23 at 11:36
1 Answers
1
You can use olevba
:
#pip install oletools
import oletools.olevba as olevba
vba = olevba.VBA_Parser("file.xlsm")
with open("output.txt", "w") as f:
for *_, n, m in vba.extract_all_macros():
if n.startswith("Module"):
print(m, file=f)
NB : If you want to target the Excel objects as well, you can use this approach.
Output (output.txt
) :
Attribute VB_Name = "Module1"
Sub Foo()
Range("B1").Select
ActiveCell.FormulaR1C1 = "StackOverflow"
Range("B2").Select
End Sub
Input used (file.xlsm
) with this code :

Timeless
- 22,580
- 4
- 12
- 30