LoginSignup
11
12

More than 5 years have passed since last update.

ExcelファイルのVBAをExportするためのVBScript

Last updated at Posted at 2017-08-21
<説明>

このVBSの上にExcelファイルをドラッグドロップすると、そのExcelファイル内のVBAコードがExportされる。
あらかじめ、Excelのオプションの「VBAプロジェクトオブジェクトモデルへのアクセルを信頼する」にチェックを入れておく必要がある。


ExportVBA.vbs
Option Explicit

Dim a   'Argument
Dim b   'Book
Dim c   'Component
Dim d   'Directory
Dim e   'Excel
Dim f   'FileSystemObject

With WScript
    If .Arguments.Count = 0 Then
        .Echo "Drop your Excel Macro Files on it."
    Else
        Set f = CreateObject("Scripting.FileSystemObject")
        Set e = CreateObject("Excel.Application")
        For Each a In .Arguments
            If f.FileExists(a) Then
                d = f.GetParentFolderName(.ScriptFullName) & "\vba_" & f.GetFileName(a) 
                Set b = e.Workbooks.Open(a,0,True,,,,True)
                With b.VBProject 
                    If .Protection = 0 Then
                        For Each c In .VBComponents
                            If c.CodeModule.CountOfLines > 2 Then
                                If Not f.FolderExists(d) And Not f.FileExists(d) Then f.CreateFolder d 
                                c.Export d & "\" & c.Name & Ext(c.Type) 
                            End If
                        Next    
                    End If
                End With
                b.Close False
            End If
        Next
        e.Quit
        Set b = Nothing
        Set e = Nothing
        Set f = Nothing
    End If
End With

Function Ext(cType)
    Select Case cType
        Case 1      : Ext = ".bas"
        Case 3      : Ext = ".frm"
        Case Else   : Ext = ".cls"
    End Select
End Function


11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12