| Class CExcel
Public App
Public WshShell
Public ErrDescription
Private WorkBook
Public CurBook
' ************************************************
' インスタンスが作成されたときの処理
' ************************************************
Private Sub Class_Initialize()
Call InitSetting()
End Sub
' ************************************************
' インスタンスが終了したときの処理
' ( Set インタンス変数 = Nothing で発生 )
' ************************************************
Private Sub Class_Terminate()
Call Quit()
End Sub
' ************************************************
' 初期処理
' ************************************************
Public Default Function InitSetting()
if IsEmpty( App ) then
Set App = CreateObject("Excel.Application")
end if
if IsEmpty( WshShell ) then
Set WshShell = CreateObject("WScript.Shell")
end if
' ユーザーに入力を促すメッセージを表示させないようにする
App.DisplayAlerts = False
Set CurBook = Nothing
end function
' ************************************************
' 終了
' ************************************************
Public Function Quit()
If not IsEmpty( App ) Then
For Each Workbook In App.Workbooks
' 全てのブックを保存した事にする
WorkBook.Saved = True
Next
App.Quit
Set App = Nothing
App = Empty
Set CurBook = Nothing
End If
End Function
' ************************************************
' 表示・非表示
' ************************************************
Public Property Let Visible( bFlg )
App.Visible = bFlg
End Property
Public Property Get Visible
Visible = App.Visible
End Property
' ************************************************
' 開く
' ************************************************
Public Function Open( strPath )
on error resume next
Set Open = App.Workbooks.Open(strPath)
if Err.Number <> 0 then
Set Open = Nothing
ErrDescription = Err.Description
Exit Function
end if
on error goto 0
Set CurBook = Open
' アクティブなウィンドウを最大化
App.ActiveWindow.WindowState = xlMaximized
End Function
' ************************************************
' 新規ブック作成
' ************************************************
Public Function Create( strPath )
Dim nBooks
App.Workbooks.Add
nBooks = App.Workbooks.Count
Set Create = App.Workbooks( nBooks )
Set CurBook = Create
CurBook.Activate
' アクティブなウィンドウを最大化
App.ActiveWindow.WindowState = xlMaximized
if strPath <> "" then
on error resume next
CurBook.SaveAs( strPath )
if Err.Number <> 0 then
MsgBox( Err.Description )
Exit Function
end if
on error goto 0
end if
End Function
' ************************************************
' 閉じる
' ************************************************
Public Function Close( MyBook )
if IsObject( MyBook ) then
MyBook.Saved = True
MyBook.Close
Set MyBook = Nothing
MyBook = Empty
else
if CurBook is Nothing then
else
CurBook.Saved = True
CurBook.Close
Set CurBook = Nothing
end if
end if
End Function
' ******************************************************
' 上書き保存
' ******************************************************
Function Save( MyBook )
if IsObject( MyBook ) then
MyBook.Save
else
CurBook.Save
end if
End Function
' ******************************************************
' 名前を付けて保存
' ******************************************************
Function SaveAs( MyBook, strPath )
if IsObject( MyBook ) then
MyBook.SaveAs strPath
else
CurBook.SaveAs strPath
end if
End Function
' ******************************************************
' LOAD
' ******************************************************
Function Load( strPath )
if not IsEmpty( App ) then
MsgBox( "Excel をロードする前に、Quitを実行して下さい " )
Exit Function
end if
Call WshShell.Run( _
"RunDLL32.EXE shell32.dll,ShellExec_RunDLL " & _
strPath _
)
End Function
End Class
| |