Option Explicit
'Remember to add a reference to Microsoft Visual Basic for Applications Extensibility
'Exports all VBA project components containing code to a folder in the same directory as this spreadsheet.
Public Sub ExportAllComponents()
Dim VBComp As VBIDE.VBComponent
Dim destDir As String, fName As String, ext As String
'Create the directory where code will be created.
'Alternatively, you could change this so that the user is prompted
If ActiveWorkbook.Path = "" Then
MsgBox "You must first save this workbook somewhere so that it has a path.", , "Error"
Exit Sub
End If
destDir = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & " Modules"
If Dir(destDir, vbDirectory) = vbNullString Then MkDir destDir
'Export all non-blank components to the directory
For Each VBComp In ActiveWorkbook.VBProject.VBComponents
If VBComp.CodeModule.CountOfLines > 0 Then
'Determine the standard extention of the exported file.
'These can be anything, but for re-importing, should be the following:
Select Case VBComp.Type
Case vbext_ct_ClassModule: ext = ".cls"
Case vbext_ct_Document: ext = ".cls"
Case vbext_ct_StdModule: ext = ".bas"
Case vbext_ct_MSForm: ext = ".frm"
Case Else: ext = vbNullString
End Select
If ext <> vbNullString Then
fName = destDir & "\" & VBComp.Name & ext
'Overwrite the existing file
'Alternatively, you can prompt the user before killing the file.
If Dir(fName, vbNormal) <> vbNullString Then Kill (fName)
VBComp.Export (fName)
End If
End If
Next VBComp
End Sub
There's room to make some changes. The behaviour that suits me best is making a new directory alongside the workbook called "(Workbook name) Modules" and filling it with all the code. This is coded in the script, but could easily be changed to some other path, or to prompt the user for the path. You can also customize the exporting extension, but I use the standard extensions excel uses so that files can be re-imported easily. Finally, I automatically overwrite any existing versions of the exported files, but you could easily arrange to prompt the user first. If using the code directly, remember to use the Reference mentioned at the top of the code.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (6)
Author
Commented:For the part where you actually create an Add-In (and add the button), that's not my specialty. I pulled it off by following the steps in the article I linked to. You should follow it closely too: http://www.fontstuff.com/vba/vbatut03.htm
Commented:
Yea, that's the part where I'll have to do some research. The page you've referenced isn't very explicit about this. Near the bottom of the page, there's a section titled:
Scott.
Author
Commented:It's not just us either. According to this xtremevbtalk thread, the existing methods of manipulating the ribbon involve programatically changing it from a COM or .NET add-in, or hacking the XML of an XLA add-in. It does give advice on how to do the latter in the simplest way possible, but it's still not very simple.
Perhaps this 'ribbon customizer' add-on would be of use?
http://word.mvps.org/faqs/customization/customizeribbon.htm
Alain
Commented:
Commented:
"Quick Access Toolbar" was the missing key....
1. Click the Office button, top left.
2. Press "Excel Options" button
3. Choose "Customize"
4. From the "Popular Commands" select list, choose: Macros
5. Select "ExportAllComponents" from the list, and click "Add".
6. Click Ok.
View More