夜間バッチ
Excelで夜間バッチを実行するには
(1)Excelファイルにそのファイルで実行するマクロを記述する。
(2)そのマクロの起動をvbスクリプトに記述する。
(3)vbスクリプトへのショートカットをスタートアップに入れる。
(4)BIOSでコンピューターの自動起動を設定する。
という手順になります。これで、毎日午前0時にパソコンが起動して、WSHがvbスクリプトを解釈して、EXCELのマクロを夜間バッチとして起動し、処理完了後にシャットダウンするというようなことができます。


//夜間バッチ.vbs
Option Explicit
Dim objFSO
Dim excelApp
Dim strNow
Dim strNow2
Dim master_from
Dim master_to
Dim ws
Dim mail_honbun
Dim oMsg
Dim currTime
Dim fromTime
Dim toTime
currTime = Time '現在時刻
fromTime = TimeValue("00:00") '時刻条件From
toTime = TimeValue("02:00") '時刻条件to
If fromTime <= currTime And toTime >= currTime Then '指定時刻範囲内のみ実行
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set excelApp = CreateObject("Excel.Application")
excelApp.Visible = True
'(1)マスタバックアップ複写
strNow = Now
strNow2=Replace(Replace(strNow, "/", "/"), ":", ":")
master_from = "\\192.168.0.1\マスタ.xlsm"
master_to = "\\192.168.0.1\backup\マスタ_" & strNow2 & ".xlsm"
Call objFSO.CopyFile(master_from, master_to)
'(2)マスタ更新
Set excelBook = excelApp.Workbooks.Open("\\192.168.0.1\マスタ.xlsm") 'Excelファイルを開く
errmsg = excelApp.Run("Excel内マクロ名")
excelBook.close True 'ブック更新箇所保存
Set excelBook = Nothing
WScript.Sleep(6000) '6秒待ち
'(3)EXCEL終了
excelApp.quit 'EXCELを終了
Set excelApp = Nothing
Set objFSO = Nothing
'(4)通知メール送信
mail_honbun = "下記の自動更新が完了しました " & Now & vbCrLf & " マスタ" & vbCrLf & errmsg
Set oMsg = CreateObject("CDO.Message")
With oMsg
.From = "FFFF@FFFF.co.jp"
.To = "TTTT@TTTT.co.jp"
.Subject = "マスタ自動更新完了"
.TextBody = mail_honbun
With .Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "XXXX@XXXX.co.jp"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "XXXX"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Update
end With
.Send
end With
Set oMsg = Nothing
WScript.Sleep(10000) '10秒待ち
'(5)シャットダウン
Set ws = WScript.CreateObject("WScript.Shell")
ws.Run "%WINDIR%\system32\shutdown.exe -s -t 60", 0 '60秒待ち
Set ws = Nothing
end if
処理結果はメール送信します。
ディスカッション
コメント一覧
まだ、コメントがありません