简体   繁体   English

Outlook 和 Excel VBA 任务计划程序

[英]Outlook and Excel VBA task Scheduler

Am in desperate need for help as this whole "system" should be up by this week but being a totally novice to vba scripts and codes etc, I have no idea how to perform the tasks.我迫切需要帮助,因为整个“系统”应该在本周启动,但作为vba 脚本和代码等方面的新手,我不知道如何执行任务。

I have created an excel which generates daily email reminders based on due dates and would like to use task scheduler to get it opened daily.我创建了一个 excel,它根据截止日期生成每日电子邮件提醒,并希望使用任务计划程序每天打开它。

What I want:我想要的是:

  1. PC to auto boot up at 745am (most likely using bios power management) PC 在早上 745 点自动启动(最有可能使用 bios 电源管理)
  2. PC reach user login page. PC 到达用户登录页面。
  3. Task scheduler opens outlook, followed by my excel and sent out the emails at 8am.任务计划程序打开outlook,然后是我的excel,并在早上8点发送电子邮件。
  4. Excel get saved and closed. Excel 被保存并关闭。 (does this need a separate macro or code within the excel?) (这是否需要在 excel 中使用单独的宏或代码?)
  5. Computer shut down using task scheduler.使用任务计划程序关闭计算机。

From what I found out from various pages/questions asked by others, a vbs/cmd script have to be written, but some sources stated that in the task scheduler to run that script, I am not supposed to tick the option to "run whether user is logged on or not" (have no Idea how to write them as well, all I know is that I have to write it in notepad and save in the specific extension for the file name) Hope someone could provide me with a detailed guide on how to perform the above tasks.从我从其他人提出的各种页面/问题中发现,必须编写一个vbs/cmd脚本,但一些消息来源指出,在任务调度程序中运行该脚本,我不应该勾选“运行是否用户是否已登录”(也不知道如何编写它们,我所知道的是我必须在记事本中编写它并保存在文件名的特定扩展名中)希望有人可以为我提供详细的指南关于如何执行上述任务。 Also, I tried to use task scheduler to open the outlook app directly but it doesn't seem to work.另外,我尝试使用任务计划程序直接打开 Outlook 应用程序,但它似乎不起作用。 Does it require a script as well?它也需要脚本吗?

Other help needed for my excel: currently, my reminder macro is running on the 1st sheet only.我的 excel 需要其他帮助:目前,我的提醒宏仅在第一张纸上运行。 Is it possible for it to run on all sheets?它可以在所有工作表上运行吗?

The code of the excel is as below: excel的代码如下:

Dim Bcell As Range
Dim iTo, iSubject, iBody As String
Dim ImportanceLevel As String

Public Sub CheckDates()

 For Each Bcell In Range("c2", Range("c" & Rows.Count).End(xlUp))

If Bcell.Offset(0, 5) <> Empty Then ' if email column is not empty then command continues
    If Now() - Bcell.Offset(0, 6) > 0.9875 Then ' mail will not be sent if current time is within 23.7 hours from time of mail last sent.
    ' Example: if mail is sent at 8am monday, between 8am monday to tuesday 7:18am, mail will not be sent.

        If DateDiff("d", Now(), Bcell) = 60 Then ' if date in column c is 60days later, email will be sent
'       Debug.Print Bcell.Row & " 60"

        iTo = Bcell.Offset(0, 5)

        iSubject = "FIRST REMINDER - IN/SSGIFR no. " & Bcell.Offset(0, -2)

        iBody = "Dear all," & vbCrLf & vbCrLf & _
        "IN/SSGIFR No. " & Bcell.Offset(0, -2) & " - " & Bcell.Offset(0, 1) & " (Batch: " & Bcell.Offset(0, 3) & ", Qty: " & _
        Bcell.Offset(0, 2) & ")" & ", notified on " & Bcell.Offset(0, -1) & " will be due on " & _
        Bcell & "." & vbCrLf & "Please ensure that the consignment is closed by the due date and forward the closure reports ASAP." & _
        vbCrLf & vbCrLf & "Thank you" & vbCrLf & vbCrLf & "Regards," & vbCrLf & "YYY Department" & _
        vbCrLf & "XXX Pte Ltd."

        SendEmail
        Bcell.Offset(0, 6) = Now()

        End If


          If DateDiff("d", Now(), Bcell) = 30 Then ' if date in column c is 30 days later, email will be sent
'         Debug.Print Bcell.Row & " 30"

          iTo = Bcell.Offset(0, 5)

          iSubject = "SECOND REMINDER - IN/SSGIFR no. " & Bcell.Offset(0, -2)

          iBody = "Dear all," & vbCrLf & vbCrLf & _
          "IN/SSGIFR No. " & Bcell.Offset(0, -2) & " - " & Bcell.Offset(0, 1) & " (Batch: " & Bcell.Offset(0, 3) & ", Qty: " & _
          Bcell.Offset(0, 2) & ")" & ", notified on " & Bcell.Offset(0, -1) & " will be due on " & _
          Bcell & "." & vbCrLf & "Please ensure that the consignment is closed by the due date and forward the closure reports ASAP." & _
          vbCrLf & vbCrLf & "Thank you" & vbCrLf & vbCrLf & "Regards," & vbCrLf & "YYY Department" & _
          vbCrLf & "XXX Pte Ltd."

          SendEmail
          Bcell.Offset(0, 6) = Now()

        End If

        If DateDiff("d", Now(), Bcell) = 7 Then ' if date in column c is 30days later, email will be sent
'       Debug.Print "ROW: " & Bcell.Row & " 7"
        iTo = Bcell.Offset(0, 5)

        iSubject = "FINAL REMINDER - IN/SSGIFR no. " & Bcell.Offset(0, -2)

        iBody = "Dear all," & vbCrLf & vbCrLf & _
        "IN/SSGIFR No. " & Bcell.Offset(0, -2) & " - " & Bcell.Offset(0, 1) & " (Batch: " & Bcell.Offset(0, 3) & ", Qty: " & _
        Bcell.Offset(0, 2) & ")" & ", notified on " & Bcell.Offset(0, -1) & " will be due on " & _
        Bcell & "." & vbCrLf & "Please ensure that the consignment is closed by the due date and forward the closure reports ASAP." & _
        vbCrLf & vbCrLf & "Thank you" & vbCrLf & vbCrLf & "Regards," & vbCrLf & "YYY Department" & _
        vbCrLf & "XXX Pte Ltd."

        SendEmail
        Bcell.Offset(0, 6) = Now()

        End If
    End If
End If
            iTo = Empty
            iSubject = Empty
            iBody = Empty
    Next Bcell

End Sub



Private Sub SendEmail()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next

    With OutMail
        .To = iTo
        .CC = "DEPARTMENT@EMAIL.COM" & ";COLLEAGUE@EMAIL.COM"
        .BCC = ""
        .Subject = iSubject
        .Body = iBody
        .Importance = ImportanceLevel
        'You can add a file like this
        '.Attachments.Add ("C:\test.txt")
        .Display
    End With

    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

Now that you have Outlook running, Lets create a Recurring Task Item with reminder and set the time that you would like to call Excel.现在您已经运行了 Outlook,让我们创建一个带有提醒的重复性任务项,并设置您想要调用 Excel 的时间。

MSDN Application.Reminder Event (Outlook) Occurs immediately before a reminder is displayed. MSDN Application.Reminder 事件 (Outlook) 在显示提醒之前立即发生。

Task Item with Reminder带提醒的任务项

在此处输入图片说明

Code goes to Outlook under ThisOutlookSession代码转到 ThisOutlookSession 下的 Outlook

Private Sub Application_Reminder(ByVal Item As Object)
    If TypeOf Item Is Outlook.TaskItem Then

        If Not Item.Subject = "Send Report" Then
            Exit Sub
        End If

    End If

    GetTemp Item ' call sub
End Sub

Private Sub GetTemp(ByVal Item As TaskItem)
    Dim xlApp As Excel.Application
    Dim xlBook As Workbook

    Set xlApp = New Excel.Application
    Set xlBook = xlApp.Workbooks.Open("C:\Temp\Excel_File.xlsm") ' update with Excel name
    xlApp.Visible = True

'   // Run Macro in Excel_File
    xlBook.Application.Run "Module1.CheckDates" ' Update with subname

    Set xlApp = Nothing
    Set xlBook = Nothing
End Sub

Update Excel Path更新 Excel 路径

xlApp.Workbooks.Open("C:\Temp\Excel_File.xlsm")

Make sure to add Excel Library object to Outlook and macro security is enable to run确保将 Excel 库对象添加到 Outlook 并启用宏安全性以运行

Tools - References then look for Microsoft Excel xxx Object Library工具 - 参考然后查找 Microsoft Excel xxx 对象库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM