Skip to content

Instantly share code, notes, and snippets.

@nmartinet
Created December 13, 2015 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nmartinet/31f0bb65842689799439 to your computer and use it in GitHub Desktop.
Save nmartinet/31f0bb65842689799439 to your computer and use it in GitHub Desktop.
extract all VBA modules from the given workbooks. Use full paths, and enable access to the vba project object model in excel
def extract_vb_modules(paths)
require 'win32ole'
excel_app = WIN32OLE.new('Excel.Application')
excel_app.ScreenUpdating = false
excel_app.EnableEvents = false
excel_app.DisplayAlerts = false
#excel_app.Calculation = -4135
paths.each do |path|
workbook = excel_app.Workbooks.open(path)
workbook.VBProject.VBComponents.each do |vb_component|
if vb_component.CodeModule.CountOfLines > 0
vb_component.Export(
"#{File.dirname(path).gsub(/\//, "\\\\")}\\#{
File.basename(path) + "_" + vb_component.codemodule.name}")
end
end
workbook.close
end
excel_app.quit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment