アットウィキロゴ

wsh_ExcelOpe

WSHによるExcel操作例

GetVBACode

ExcelのVBAのコードを取得する。
下記のExcelプラグインの改造を行いました。
http://www.geocities.co.jp/SiliconValley-SanJose/8165/winmerge.html

現在、basとclsしか取得していません。frmやWorkSheetはファイルに落していないです。

引数

1 Excelファイル名
2 出力先ディレクトリ

コード

<?xml version="1.0" encoding="Shift_JIS" standalone="yes" ?>
<package>
<job id="GetVBACode">
<?job error="True" debug="True" ?>
<script language="VBScript">
<![CDATA[
	Dim fso			' FileSystemObject
	Dim fo			' 出力ファイル
	Dim xl			' Excelオブジェクト
	Dim wbk			' ワークブック
	Dim cmp			' VBProject.VBComponents
	
	Dim fileSrc		' 検査対象のXLS
	Dim dirDst		' 保存先のディレクトリ
	Dim fileDst 	' 保存先のファイル
	Dim sFormat		' 拡張子

	' Excelの取得.	
	fileSrc = WScript.Arguments(0)

	' 保存先のディレクトリを取得
	dirDst  = WScript.Arguments(1)
	

	Set fso = CreateObject("Scripting.FileSystemObject")
	Set xl = CreateObject("Excel.Application")
	Set wbk = xl.Workbooks.Open(fileSrc)

	xl.DisplayAlerts = False

	On Error Resume Next

	For Each cmp In wbk.VBProject.VBComponents
		
		Select Case cmp.Type
			Case 1 
				sFormat = "bas"
			Case 2
				sFormat = "cls"
			Case Else
				sFormat = ""
		End Select
		If sFormat <> "" Then
			fileDst = dirDst + "\" + cmp.Name + "." + sFormat
			Set fo = fso.CreateTextFile(fileDst, True)
			If cmp.CodeModule.CountOfLines > 0 Then
				fo.WriteLine cmp.CodeModule.Lines(1, cmp.CodeModule.CountOfLines)
			End If
			fo.WriteLine ""
			fo.Close
		End If
	Next

	wbk.Close
	Set wbk = Nothing
	xl.Quit
	Set xl = Nothing
	Set fo = Nothing
	Set fso = Nothing
]]>
</script>
</job>
</package> 
最終更新:2010年04月13日 14:48
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。