我正在尝试在任何特定培训到期日期的45天内向员工发送自动电子邮件,而不必打开文件来运行宏(如果可能)。此外,在没有每次培训的电子邮件预览的情况下完成此操作,因为每次运行宏时可能会弹出数百个。
我想从我的一列中提取确定电子邮件正文的培训的名称。
我想从一个单独的Excel文件中提取到期日期,该文件是培训日期的记录。
我设法在培训到期后的45天内对培训的电子邮件预览进行了排队,但是代码允许Excel发送关于已经到期的培训的提醒。
我希望Excel在到期日到期后立即停止。我尝试过:If mydate2 - datetoday2 <= 45 Then。这是可行的,但会向不活跃的员工发送电子邮件,但培训记录文件中仍有他们的培训记录。
我尝试从列中提取培训名称,并让它们自动填充到电子邮件正文中。例如,电子邮件将显示"Training due to Expire:“,然后让excel根据第4列中的值填写培训名称。
Sub datesexcelvba()
Dim myapp As Outlook.Application, mymail As Outlook.MailItem
Dim mydate1 As Date
Dim mydate2 As Long
Dim datetoday1 As Date
Dim datetoday2 As Long
Dim x As Long
lastrow = Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lastrow
mydate1 = Cells(x, 5).Value
mydate2 = mydate1
Cells(x, 11).Value = mydate2
datetoday1 = Date
datetoday2 = datetoday1
Cells(x, 12).Value = datetoday2
If mydate2 - datetoday2 <= 45 Then
Set myapp = New Outlook.Application
Set mymail = myapp.CreateItem(olMailItem)
mymail.To = Cells(x, 6).Value
With mymail
.Subject = "Training Expiration Reminder"
.Body = "Please contact your supervisor to enroll in the next possible recertification class. Training Expiring:" value=x3
.Display
'.send
End With
Cells(x, 7) = "REMINDER SENT"
Cells(x, 7).Font.Bold = True
Cells(x, 13).Value = mydate2 - datetoday2
End If
Next
Set myapp = Nothing
Set mymail = Nothing
End Sub发布于 2019-08-14 03:25:37
张贴评论作为答案,所以这可以作为答案列出。
.Body = "..." value=x3 需要的是
.Body = "..." & cells(x,3).value,或者类似于您的上下文。
关于你关于提示/要点的问题,不要害怕对你的代码进行划分,这样就更容易处理这些部分。
你的代码有2到3个部分,你可以像下面这样调用不同的部分:
Public Sub execute()
If ActiveSheet.name <> "SQDC" Then Exit Sub
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.Calculation = xlManual
End With
'''
task_one
task_two
task_three
'''
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.Calculation = xlAutomatic
End With
End Sub关于来回使用Outlook和Excel,请将关键项另存为变量,以便Outlook可以执行所需的操作,而无需搜索单元格引用。
'at the top of the module, outside of the subroutine:
public email_address as string, employ_name as string
'where If mydate2 - datetoday2 <= 45 Then is TRUE
email_address = cells(x,6).value
employ_name = cells(x,3).value然后,您可以在代码的电子邮件部分使用它们。假设您将excel活动与outlook活动分开,所以全局变量在那里,这样您就可以在一个子例程中定义它们,并在另一个子例程中使用它们。
帮助我在VBA中变得平庸的一件事是阅读别人的问题并试图回答他们(即使只是在你的脑海中),通过找到正确的术语等等。最终你会有足够的知识来与他们中最好的人斗争!
https://stackoverflow.com/questions/57483470
复制相似问题