Outlook和Excel VBA任务计划程序

时间:2016-10-19 01:01:38

标签: excel vba excel-vba outlook outlook-vba

我迫切需要帮助,因为整个"系统"应该在本周开始,但对 vba脚本和代码等完全是新手,我不知道如何执行任务。

我创建了一个excel,它根据截止日期生成每日电子邮件提醒,并希望使用任务计划程序每天打开它。

我想要的是什么:

  1. PC在早上7点45分自动启动(最有可能使用bios电源管理)
  2. PC到达用户登录页面。
  3. 任务计划程序打开outlook,然后是我的excel,并在上午8点发送电子邮件。
  4. Excel保存并关闭。 (这需要在Excel中使用单独的宏或代码吗?)
  5. 使用任务计划程序关闭计算机。
  6. 从我从其他人提出的各种页面/问题中发现,必须编写 vbs / cmd 脚本,但有些消息来源表示在任务调度程序中运行该脚本,我是不应该勾选选项"运行用户是否登录" (不知道怎么写它们,我所知道的是我必须在记事本中写它并保存在文件名的特定扩展名中) 希望有人能为我提供有关如何执行上述任务的详细指南。 此外,我尝试使用任务计划程序直接打开Outlook应用程序,但它似乎不起作用。它是否也需要脚本?

    我的Excel所需的其他帮助:目前,我的提醒宏仅在第一张纸上运行。它可以在所有纸张上运行吗?

    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
    

1 个答案:

答案 0 :(得分:1)

现在您已运行Outlook,让我们创建一个带有提醒的重复任务项,并设置您想要调用Excel的时间。

MSDN Application.Reminder Event (Outlook) Occurs immediately before a reminder is displayed.

带提醒的任务项

enter image description here

代码在ThisOutlookSession

下转到Outlook
xlApp.Workbooks.Open("C:\Temp\Excel_File.xlsm")

更新Excel路径

SEGFAULT

确保将Excel库对象添加到Outlook,并启用宏安全性

工具 - 参考然后查找Microsoft Excel xxx对象库