LoginSignup
16
19

More than 5 years have passed since last update.

EXCELのVBAソースをExportする

Last updated at Posted at 2013-11-30

EXCEL VBAを勉強しています。サンプルソースが1つ1つのExcelに保存されているため、後日調べる時は不便だなあと思って、Excelに保存しているソースをExportすることにしました。本当はDBやら、HTMLやらに保存し、キーワードなどで検索してすぐ使えるのがベストです。宿題にしておきます。

使い方:
バッチファイルExportExcelModule.batとCScript ExportExcelModule.vbsを
VBAソースを記述したExcelファイルの保存フォルダの親フォルダに格納し、
ソースを保存するフォルダを事前に作成し、バッチファイルのEXPORT_PATH変数に設定した後に、ExportExcelModule.batを実行したら、後は待つだけです。ソースは下記の通りです。


ExportExcelModule.bat
@echo off

rem ---------------------------------------------------------------
rem 機能:VBAのソースをExportする
rem ---------------------------------------------------------------
rem 使い方:
rem     当バッチファイルと下記呼び出しているExportExcelModule.vbsを
rem     VBAソースを記述したExcelファイルの保存フォルダの親フォルダに格納する
rem ---------------------------------------------------------------

rem VBAのソースの保存箇所を設定
set EXPORT_PATH="C:\ExcelVBA_SOURCE\Export"

rem このバッチが存在するフォルダをカレントに移動
pushd %0\..

cls


rem --------------------------------------------------------------------
rem カレントフォルダとサブフォルダに含めている全てのEXCEL(xlsmも対象になる)をループし、
rem ExportExcelModule.vbsでソースをエクスポートする
rem (ループはここでしているため、若干性能が悪いが...VBSで再帰処理を書かなくて済む。)
rem --------------------------------------------------------------------
for /F "usebackq" %%i in (`dir /s /b *.xls `) do ( 
    echo %%i 
    CScript ExportExcelModule.vbs %%i %EXPORT_PATH%
    rem pause
)
pause
exit

rem --------------------------------------------------------------------
rem 勉強時メモ
rem メモ1:カレントディレクトリの拡張子がxlsのファイルを出力
rem    for %%i in (*.xls) do ( echo %%i )
rem メモ2(正規表現が利かなかった):
rem for /F "usebackq" %%i in (`dir /s /b *.xls ^| findstr /V ".*\.xls$" `) do ()
rem --------------------------------------------------------------------
ExportExcelModule.vbs
Dim objParams, strFullPath, strFileName, objExcel, objWorkBook
Dim objTempComponent, strExportPath, strCode 
Dim FSO

strFullPath=""
strExportPath = ""
strFileName = ""
strFilePath = ""

Set FSO = CreateObject("Scripting.FileSystemObject")
Set objParams = WScript.Arguments
If objParams.Count = 2 Then
    strFullPath = objParams.Item(0)
    strExportPath = objParams.Item(1)
    strFileName = FSO.GetFileName(strFullPath)
    strFilePath = FSO.GetParentFolderName(strFullPath)
    'WScript.Echo "strFullPath---->" & strFullPath
    'WScript.Echo "strFileName---->" & strFileName
    'WScript.Echo "strFilePath---->" & strFilePath
    'WScript.Echo "strExportPath---->" & strExportPath

Else
    WScript.Quit 0
End If

'Excel操作準備
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
objExcel.EnableEvents = False
'マクロが無効の状態で開く
'※だめだった!!objExcel.AutomationSecurity = msoAutomationSecurityForceDisable
Set objWorkBook = objExcel.Workbooks.Open(strFullPath)

'ソースをエクスポートする
Call ExportSource()

'Excelをクローズ
Set FSO = nothing
Set objParams = nothing
objExcel.DisplayAlerts = True
objExcel.EnableEvents = True
objWorkBook.Close False
objExcel.Quit
Set objWorkBook = nothing
Set objExcel = nothing

'--------------------------------------------------------------------------
'ソースをエクスポートする
'--------------------------------------------------------------------------
Sub ExportSource()
    For Each TempComponent In objWorkBook.VBProject.VBComponents
        If TempComponent.CodeModule.CountOfDeclarationLines <> TempComponent.CodeModule.CountOfLines Then
            Select Case TempComponent.Type
                'STANDARD_MODULE
                Case 1
                    TempComponent.Export strExportPath & "\" & objWorkBook.Name & "_" & TempComponent.Name & ".bas"
                'CLASS_MODULE
                Case 2
                    TempComponent.Export strExportPath & "\" & objWorkBook.Name & "_" & TempComponent.Name & ".cls"
                'USER_FORM
                Case 3
                    TempComponent.Export strExportPath & "\" & objWorkBook.Name & "_" & TempComponent.Name & ".frm"
                'SHEETとThisWorkBook
                Case 100
                    TempComponent.Export strExportPath & "\" & objWorkBook.Name & "_" & TempComponent.Name & ".bas"
            End Select
            With TempComponent.CodeModule
                'Code = .Lines(1, .CountOfLines)
                'Code = .Lines(.CountOfDeclarationLines + 1, .CountOfLines - .CountOfDeclarationLines + 1)                    
            End With
        End If
    Next

End Sub
16
19
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
16
19