Imports System.IO
Public Sub Main()
RunScheduledTask()
End Sub
Sub RunScheduledTask()
Const strControlFileName As String = "X:\Folder\Task01.xlsm"
Dim oExcel As Microsoft.Office.Interop.Excel.Application
Dim oBook As Microsoft.Office.Interop.Excel.Workbook
Dim oBooks As Microsoft.Office.Interop.Excel.Workbooks
Dim strFilenameCheck As String
Dim strMacroName As String
Try
strMacroName = "RunTask01"
oExcel = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
oExcel.Visible = False
oBooks = CType(oExcel.Workbooks(), Microsoft.Office.Interop.Excel.Workbooks)
strFilenameCheck = Dir(strControlFileName)
If strFilenameCheck <> "" Then
oBook = CType(oBooks.Open(strControlFileName), Microsoft.Office.Interop.Excel.Workbook)
oExcel.DisplayAlerts = False
oExcel.Run(strMacroName)
oExcel.DisplayAlerts = True
Else
Dim sw As New StreamWriter(Application.StartupPath & "\ScheduledTask_error.log", True)
sw.WriteLine(Now() & " - '" & strControlFileName & "' could not be accessed.")
sw.Close()
End
End If
'
Catch ex As Exception
Dim sw As New StreamWriter(Application.StartupPath & "\ScheduledTask_Error.log", True)
sw.WriteLine(Now() & " - " & ex.Message)
sw.Close()
Finally
oBook.Close(False)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
oBook = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
oBooks = Nothing
oExcel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
oExcel = Nothing
GC.Collect()
End Try
End Sub
End Module