无法让 VBA 创建的计划任务在 PC 睡眠时工作

问题描述 投票:0回答:1

我创建了一个使用VBA生成Windows计划任务的进程。 该任务在指定的日期/时间启动 Excel 工作簿并执行其中包含的 VBA 宏。 该任务是一次性执行任务。 宏完成后,工作簿将保持打开状态。 该过程有效,但仅当电脑在指定的日期/时间没有休眠时才有效。 我的环境是Windows 11和Office 365。任务历史记录显示该任务确实执行,但如果PC处于睡眠状态,则永远不会打开excel工作簿。 我已将 wakeToRun 的参数设置为 true。 我已尝试此处显示的示例,该示例配置为安排任务以交互式令牌类型登录(3)

我也尝试过输入密码(1)。 当使用类型 1 并指定我的 PC 登录名/密码时,任务会超时,并且即使 PC 未处于睡眠状态,也不会打开工作簿。这是注册任务的行:

Call rootFolder.RegisterTaskDefinition("Book1", taskDefinition, 6, , , 3)

如有任何建议,我们将不胜感激。 将示例代码粘贴到工作簿中并另存为 Book1。 定义两个命名范围,StartDtStartTm,每个引用一个单独的单元格,该单元格将保存执行开始日期/时间。

Sub CreateVBSFile()
    ' only need to do this once
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    Dim fileStream As TextStream
    Set fileStream = fso.CreateTextFile(Environ("TEMP") & "\MyTestRun.vbs")
    Dim sS As String
    ' use book1.xlsm as name for the workbook
      sS = "ExcelFilePath = """ & ThisWorkbook.FullName & """"
    fileStream.WriteLine sS
      sS = "MacroPath = ""Module1.testHarness"""
    fileStream.WriteLine sS
    
      sS = "Set ExcelApp = CreateObject(""Excel.Application"")"
    fileStream.WriteLine sS
    
      sS = "ExcelApp.Visible = True"
    fileStream.WriteLine sS
    
      sS = "ExcelApp.DisplayAlerts = False"
    fileStream.WriteLine sS
    
      sS = "Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)"
    fileStream.WriteLine sS
    
      sS = "ExcelApp.Run MacroPath"
    fileStream.WriteLine sS
    
    sS = "wb.Save"
    fileStream.WriteLine sS
    
    sS = "ExcelApp.DisplayAlerts = True"
    fileStream.WriteLine sS
    
    fileStream.Close
    Set fileStream = Nothing
    Set fso = Nothing
End Sub
Sub createTheSchedule()
        
    Cells(3, 3) = ""
    'value the start time to be 10 minutes from now, or what ever time you need to test when sleeping
    Range("StartTm") = Format(DateAdd("s", 600, Now), "hh:mm ampm")
    Range("StartDt") = Format(Date, "mm/dd/yyyy")
    Const TriggerTypeTime = 1
    Const ActionTypeExec = 0
    Dim service As Object
    Set service = CreateObject("Schedule.service")
    Call service.Connect
'********************************************************
' Get a folder to create a task definition in.
    Dim rootFolder
    Set rootFolder = service.GetFolder("\")
    Dim taskDefinition
    Set taskDefinition = service.NewTask(0)
' creating the RegistrationInfo object.
    Dim regInfo
    Set refInfo = taskDefinition.RegistrationInfo
    refInfo.Author = "Administrator"
    
' creating a TaskSettings object.
    Dim settings
    Set settings = taskDefinition.settings
    settings.Enabled = True
    settings.WakeToRun = True
    settings.StartWhenAvailable = True
    settings.Hidden = False
    settings.idlesettings.RestartOnIdle = True
    settings.idlesettings.StopOnIdleEnd = False
' Create a time-based trigger.
    Dim triggers, trigger
    Set triggers = taskDefinition.triggers
    Set trigger = triggers.Create(TriggerTypeTime)
    Dim startTime
    Dim time
    ' set your start date time using values in the workbook
    time = CDate(Range("StartDt")) + CDate(Range("StartTm"))
    startTime = XmlTime(time)
    trigger.StartBoundary = startTime
    trigger.ExecutionTimeLimit = "PT5M"    'Five minutes
    trigger.ID = "timeTriggerID"
    trigger.Enabled = True
' Add an action to the task to run the workbook
    Dim Action
    Set Action = taskDefinition.Actions.Create(ActionTypeExec)
    Action.Path = Environ("TEMP") & "\MyTestRun.vbs"
' Register (create) the task.
    Call rootFolder.RegisterTaskDefinition("Book1", taskDefinition, 6, , , 3)
        
End Sub
Function XmlTime(t)
    Dim cSecond, cMinute, CHour, cDay, cMonth, cYear
    Dim tTime, tDate

    cSecond = "00"
    cMinute = "0" & Minute(t)
    CHour = "0" & Hour(t)
    cDay = "0" & Day(t)
    cMonth = "0" & Month(t)
    cYear = Year(t)

    tTime = Right(CHour, 2) & ":" & Right(cMinute, 2) & _
        ":" & Right(cSecond, 2)
    tDate = cYear & "-" & Right(cMonth, 2) & "-" & Right(cDay, 2)
    XmlTime = tDate & "T" & tTime
End Function
Sub testHarness()
    Cells(3, 3) = "This ran successfully at " & Format(Now(), "hh:mm:ss ampm")
End Sub

编辑: 我发现一篇文章解释说 Windows 2008 在创建程序/脚本值超过 30 个字符的计划任务时存在问题,并附有处理该问题的说明。 然后我注意到 VBA 脚本创建的计划任务将任务设置为针对 windows server 2008 配置。 我使用任务计划程序编辑器手动将该属性修改为 Windows 10

enter image description here

enter image description here

IT 现在将在 PC 睡眠时按预期运行。 但是,我找不到任何有关如何通过 VBA 设置该属性的文档,并且只能在创建任务后对其进行修改。 有人知道如何设置该属性吗?

excel vba scheduled-tasks
1个回答
0
投票

将计划任务配置为在 Windows 10 中运行的设置是为 settings

添加一条语句

settings.compatibility = 6

感谢 AI 给我指出了这个解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.