Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dsisnero
Forked from nmartinet/extracVBAModules.rb
Created April 13, 2016 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsisnero/14040cf831083da24afe19da24013c72 to your computer and use it in GitHub Desktop.
Save dsisnero/14040cf831083da24afe19da24013c72 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