我通常需要在抵达办公室之前每天运行一个流程。我想将其设置为计划任务。
如何实现这一目标?
这样做有最好的做法吗?
可以或应该以编程方式完成吗?
答案 0 :(得分:3)
要解决此问题,我执行了以下操作:
创建名为“提交”的宏。
转到:
创建计划任务.job
文件
Start > All Programs > Accessories > System Tools > Schedule Tasks
(这会在以下位置生成.job
文件:“C:\ WINDOWS \ Tasks \ Submit.job”)
创建完成后,我将以下语法放入Run:
文本框。
"C:\Program Files\Microsoft Office\Office11\MSACCESS.EXE" "C:\MyDatabasePath\MyDatabaseName.mdb" /x "Submit"
此后,设置的剩余部分将完成,因为正常的计划任务应该是。您可以找到有关如何手动设置这些任务的更多详细信息here,或者如果您希望通过命令行执行设置,this是一个特别有用的参考。
注意:必须设置宏和作业文件才能使其正常工作。
至于最佳做法,我发现this site包含了大量有用的信息,以及何时以及如何使用此工具,Microsoft的补充。
以编程方式完成此操作的一种方法是使用作业API。以下是使用VBA完成此操作的一个示例:
Option Explicit
' Schedule api's
Declare Function NetScheduleJobAdd Lib "netapi32.dll" _
(ByVal Servername As String, Buffer As Any, Jobid As Long) As Long
' Schedule structure
Type AT_INFO
JobTime As Long
DaysOfMonth As Long
DaysOfWeek As Byte
Flags As Byte
dummy As Integer
Command As String
End Type
' Schedule constants
Const JOB_RUN_PERIODICALLY = &H1
Const JOB_NONINTERACTIVE = &H10
Const NERR_Success = 0
Private Sub Command1_Click()
Dim lngWin32apiResultCode As Long
Dim strComputerName As String
Dim lngJobID As Long
Dim udtAtInfo As AT_INFO
' Convert the computer name to unicode
strComputerName = StrConv(Text1.Text, vbUnicode)
' Setup the tasks parameters
SetStructValue udtAtInfo
' Schedule the task
lngWin32apiResultCode = NetScheduleJobAdd(strComputerName, udtAtInfo, lngJobID)
' Check if the task was scheduled
If lngWin32apiResultCode = NERR_Success Then
MsgBox "Task" & lngJobID & " has been scheduled."
End If
End Sub
Private Sub SetStructValue(udtAtInfo As AT_INFO)
Dim strTime As String
Dim strDate() As String
Dim vntWeek() As Variant
Dim intCounter As Integer
Dim intWeekCounter As Integer
vntWeek = Array("M", "T", "W", "TH", "F", "S", "SU")
With udtAtInfo
' Change the format of the time
strTime = Format(Text2.Text, "hh:mm")
' Change the time to one used by the api
.JobTime = (Hour(strTime) * 3600 + Minute(strTime) * 60) * 1000
' Set the Date parameters
If Val(Text3.Text) > 0 Then
' Set the task to run on specific days of the month i.e. 9th & 22nd of the month
strDate = Split(Text3.Text, ",")
For intCounter = 0 To UBound(strDate)
.DaysOfMonth = .DaysOfMonth + 2 ^ (strDate(intCounter) - 1)
Next
Else
' Set the task to run on sepecific days of the week i.e. Monday & Thursday
strDate = Split(Text3.Text, ",")
For intCounter = 0 To UBound(strDate)
For intWeekCounter = 0 To UBound(vntWeek)
If UCase(strDate(intCounter)) = vntWeek(intWeekCounter) Then
.DaysOfWeek = .DaysOfWeek + 2 ^ intWeekCounter
Exit For
End If
Next
Next
End If
' Set the interactive property
If Check1.Value = vbUnchecked Then
.Flags = .Flags Or JOB_NONINTERACTIVE
End If
' Set to run periodically
If Option2.Value = True Then
.Flags = .Flags Or JOB_RUN_PERIODICALLY
End If
' Set the command to run
.Command = StrConv(Text4.Text, vbUnicode)
End With
End Sub
答案 1 :(得分:1)
如果任务运行不正确,可能是由于软件和/或服务包的更新。检查Microsoft Office和Access的程序文件。根据程序文件中显示的文件夹,运行行中显示的“Office11”可能需要更改为“Office 14”或“Office12”。