2

Is it possible to extract the text of VBA source code from Office documents? Do I need to enable editing first to even make the macro code visible?

Background:

I'm developing an small application in VB6 with the purpose of opening an Excel or Word file and copy the Macro code (if any) into a textbox to investigate. I work for a company that receives a lot of requests to forward blocked mails. These mails are blocked because they have the .xls, xlsm, doc, docx, et cetera extensions. The problem is, is that I need to enable "Enable editing" for viewing the code and I don't want to do that, because if there is any code behind it, it will run.

Maarten van Stam
  • 1,901
  • 1
  • 11
  • 16
Sub
  • 55
  • 1
  • 9
  • I think, that [link](http://stackoverflow.com/questions/17536607/read-vba-macros-or-vbaproject-bin-of-an-excel-file-without-opening-it-in-ms-ex) is relevant to your question, but it seems like only _xlsm_ and _docm_ are legit for this kind of surgery since only them contain vbaProject.bin file. – CommonSense Mar 03 '17 at 11:28

1 Answers1

1

Clicking "Enable editing" will not enable macro code to run, as long as you do not set the Macro security to "Enable all macros".

Set it to the second option (Disable all macros with notification) and you will be in control.

To make it even more secure, disable Trusted Locations and trusted documents on the machine you are doing this on.

jkpieterse
  • 2,727
  • 1
  • 9
  • 18
  • Okay, thanks, but can I use VB Code to copy the macro-code to a textbox within my application without opening the excel/word file itself? – Sub Mar 03 '17 at 10:49
  • Well, it wouldn't be simple because the VBAproject is a binary file within the zip container of an Office file. You'd have to find out how to unravel that binary. – jkpieterse Mar 03 '17 at 14:47
  • See: https://www.codeproject.com/articles/15216/office-2007-bin-file-format – jkpieterse Mar 03 '17 at 14:50