Skip to content

Instantly share code, notes, and snippets.

@u1f992
Last active April 22, 2023 05:32
Show Gist options
  • Select an option

  • Save u1f992/8db93071bd25dd538b8ad1a531c8fc5c to your computer and use it in GitHub Desktop.

Select an option

Save u1f992/8db93071bd25dd538b8ad1a531c8fc5c to your computer and use it in GitHub Desktop.
VBComponentsをエクスポートする
Sub Main(ByVal args)
If args.Count = 0 Then
WScript.Echo("ExportVBComponents.vbs is the most simple solution to manage your VBA project with a version control system." & vbCrLf & _
vbCrLf & _
"Useage" & vbTab & " : [ engine ] ExportVBComponents.vbs [ xlsmfile ]" & vbCrLf & _
vbTab & "engine" & vbTab & " : Usually you can choose ""cscript"" or ""wscript""." & vbCrLf & _
vbTab & "xlsmfile" & vbTab & " : An excel file with VBA." & vbCrLf & _
"...Or just Drag'n'Drop xlsm file to this script." & vbCrLf & _
vbCrLf & _
"Caution" & vbTab & " : " & vbCrLf & _
"As a specification, all text files in ./src are deleted and re-exported every time, even if the content is completely the same." & vbCrLf & _
"The source codes exported from VBProject are not recommended to edit with a non-default editor.")
WScript.Quit
End If
Dim i, j
Dim msg
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim objExcel: Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
For i = 0 To args.Count - 1
Dim fn: fn = args.Item(i)
If fso.GetExtensionName(fn) = "xlsm" Then
Call CreateEnv(fso.GetParentFolderName(fn))
msg = fn & vbCrLf
objExcel.Workbooks.Open(fn)
Dim objVBAProject: Set objVBAProject = objExcel.VBE.VBProjects(1)
Dim objVBComponent
For Each objVBComponent In objVBAProject.VBComponents
Dim tmp: tmp = fso.GetParentFolderName(fn) & "\src\" & objVBComponent.Name & "."
Select Case objVBComponent.Type
Case 1
tmp = tmp & "bas"
Case 2
tmp = tmp & "cls"
Case 3
tmp = tmp & "frm"
Case 100
tmp = tmp & "dcm"
Case Else
WScript.Echo("Unknown type VBComponent has been detected. : " & objVBComponent.Type & vbCrLf & _
"This will be saved without extension.")
End Select
Call objVBComponent.Export(tmp)
msg = msg & " > " & fso.GetFile(tmp).Name & vbCrLf
Next
WScript.Echo(msg)
End If
Next
objExcel.Quit
Set fso = Nothing
Set objExcel = Nothing
End Sub
Sub CreateEnv(ByVal strFolder)
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(strFolder & "\src") = True Then
Call fso.DeleteFolder(strFolder & "\src", True)
End If
Call fso.CreateFolder(strFolder & "\src")
Set fso = Nothing
End Sub
Call Main(WScript.Arguments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment