如何在Windows XP中设置计划任务以在ms-access中运行宏?

发布于 2024-11-18 08:55:43 字数 104 浏览 6 评论 0原文

我通常需要每天在到达办公室之前运行一个流程。我想将其设置为计划任务。

如何才能做到这一点?

这样做有最佳实践吗?

这可以或者应该以编程方式完成吗?

I typically have some need to run a process each day prior to my arrival at the office. I would like to set this up as a scheduled task.

How can this be accomplished?

Is there a best practice on doing this?

Can or should this be done programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Spring初心 2024-11-25 08:55:43

为了解决这个问题,我执行了以下操作:

  • 创建了一个名为“Submit”的宏。

  • 通过转到以下位置创建计划任务 .job 文件:

    开始>所有程序>配件>系统工具>计划任务

计划任务对话框

(这会在以下位置:“C:\WINDOWS\Tasks\Submit.job”)

创建后,我将以下语法放入 Run: 文本框中。

"C:\Program Files\Microsoft Office\Office11\MSACCESS.EXE"  "C:\MyDatabasePath\MyDatabaseName.mdb" /x "Submit"

此后,设置的剩余部分将按照正常计划任务的方式完成。您可以在[此处][2]找到有关如何手动设置这些任务的更多详细信息,或者如果您想通过命令行执行设置,这是一个特别有用的参考。

注意:必须设置宏和作业文件才能正常工作。

以编程方式完成此任务的一种方法是利用作业 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

To resolve this I did the following:

  • Created a Macro named "Submit".

  • Created a Scheduled task .job file by going to:

    Start > All Programs > Accessories > System Tools > Schedule Tasks

Scheduled Task Dialog Box

(This produced the .job file in the following location: "C:\WINDOWS\Tasks\Submit.job")

Once this was created, I placed the following syntax into the Run: text box.

"C:\Program Files\Microsoft Office\Office11\MSACCESS.EXE"  "C:\MyDatabasePath\MyDatabaseName.mdb" /x "Submit"

After this, the remaining portion of the setup is completed as a normal schedule task should be. You can find more details about how to manually setup these tasks [here][2] or if you like to perform the setup through the command line, this is a particularly useful reference.

Note: Both the Macro and the job file must be setup for this to work correctly.

One way to accomplish this programmatically would be do utilize the job API's. Here is one such example where this was accomplished using VBA:

See Reference Here

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
岛歌少女 2024-11-25 08:55:43

如果任务无法正常运行,可能是由于软件和/或服务包更新所致。检查 Microsoft Office 和 Access 的程序文件。运行行中显示的“Office11”可能需要更改为“Office 14”或“Office12”,具体取决于程序文件中显示的文件夹。

If the tasks do not run appropriately, it may be due to updates in software and/or service packs. Check the program files for Microsoft Office and Access. "Office11" as shown in the run line may need to be changed to "Office 14" or "Office12" depending on the folders showing in program files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文