2

My requirement is: I have been given an excel (the user uploads it to our server) and then my program should automatically add a macros code (defined in a text file maybe) to the excel file and then send it back to the user. I found a similar question but the solution only works in Windows but since our server is Linux based, I haven't found a way to do so.

Link to the similar question: Use Python to Inject Macros into Spreadsheets

6
  • If you put the macro in the Personal Workbook, it is then available to all workbooks. Commented Jul 6, 2018 at 5:32
  • My requirement is: I have been given an excel (the user uploads it to our server) and then my program should automatically add a macros code (defined in a text file maybe) to the excel file and then send it back to the user. Since our server is Linux based, I haven't found a way to do so. I think my question wasn't clear enough so I have changed it accordingly Commented Jul 6, 2018 at 5:40
  • why not make an installer code - less file transfers... Commented Jul 6, 2018 at 5:45
  • It is an additional feature that is being added to an existing web application that the users are already using and I think it would be better to keep it all in the web application. Commented Jul 6, 2018 at 5:55
  • I would not EVER send you an excel file with all my work / functions just for you to add a macro - you get to keep all my material... Commented Jul 6, 2018 at 5:58

1 Answer 1

1

Assuming you're being sent a file in xlsm format, you need to following capabilities:

  1. Open the file as a zip file
  2. Locate the .bin part path from the rels files - see Microsoft Open Packaging Conventions
  3. Locate and open the VBA project's .bin stream
  4. parse the .bin stream as a Compound Binary File Format file
  5. Parse the binary streams that describe and list the module contents of the file, as documented in Office VBA File Format Structure
  6. Add your module text as a new stream, and update the files from step 5 with the new contents.

It's not a small undertaking. The work has already been done in Python, and a lot of the libraries for working with zip files and compound binary format files are already in .NET for Windows. Otherwise, as far as I'm aware, there aren't any other pre-built tools, other than the tools from aspose

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

1 Comment

looks like a lot of work! openpyxl.readthedocs.io could already help a lot, no?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.