Option Explicit
Private Declare Function NetScheduleJobAdd Lib "netapi32.dll" (ByVal Servername As String, Buffer As Any, JobID As Long) As Long
Private Type AT_INFO
JobTime As Long ' The time is the local time at a computer on which the schedule service is running;
' it is measured from midnight, and is expressed in milliseconds but is only accurate to the minute
DaysOfMonth As Long ' bit for each day of month
DaysOfWeek As Byte ' bit for each day of week
Flags As Byte ' See enums
Command As String ' Unicode command string you want to run
End Type
Private Enum JobAdd
JOB_RUN_PERIODICALLY ' If you set this flag, the job runs, and continues to run, on each
' day for which a corresponding bit is set in the DaysOfMonth or
' DaysOfWeek member. The job is not deleted after it executes.
' If this flag is clear, the job runs only once for each bit set in
' these members. The job is deleted after it executes once.
JOB_ADD_CURRENT_DATE ' If you set this flag, the job executes at the first occurrence of
' JobTime at the computer where the job is queued.
' Setting this flag is equivalent to setting the bit for the current
' day in the DaysOfMonth member.
JOB_NONINTERACTIVE
End Enum
Private Enum sjWeekdays
Monday = 1
Tuesday = 2
Wednesday = 4
Thursday = 8
Friday = 16
Saturday = 32
Sunday = 64
End Enum
Private Enum sjDays
d1 = 1
d2 = 2
d3 = 4
d4 = 8
d5 = 16
d6 = 32
d7 = 64
d8 = 128
d9 = 256
d10 = 512
d11 = 1024
d12 = 2048
d13 = 4096
d14 = 8192
d15 = 16384
d16 = 32768
d17 = 65536
d18 = 131072
d19 = 262144
d20 = 524288
d21 = 1048576
d22 = 2097152
d23 = 4194304
d24 = 8388608
d25 = 16777216
d26 = 33554432
d27 = 67108864
d28 = 134217728
d29 = 268435456
d30 = 536870912
d31 = 1073741824
End Enum
Private Function vbScheduleJob(strCommand As String, sjTime As Date, AddFlags As JobAdd, Optional DayOfWeek As sjWeekdays = 0, Optional DayOfMonth As sjDays = 0, Optional PCName As String = vbNullString) As Long
Dim myInfo As AT_INFO
Dim JobID As Long
myInfo.Command = StrConv(strCommand, vbUnicode)
myInfo.Flags = AddFlags
myInfo.JobTime = DateDiff("s", "00:00:00", Format(sjTime, "hh:mm:ss")) * 1000
myInfo.DaysOfWeek = DayOfWeek
myInfo.DaysOfMonth = DayOfMonth
NetScheduleJobAdd PCName, myInfo, JobID
vbScheduleJob = JobID
End Function