这是我的第一个问题,它是由于尝试过低而未能自行解决或通过在线研究解决。
我写了一个代码(即使用我在网上找到的代码),只要在M栏中写下“是”,就可以从Excel发送自动化的电子邮件。
请找到以下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("M2:M1000")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Call SendmailAuto
End If
End Sub
并且
Sub SendmailAuto()
Dim OutApp As Object
Dim OutMail As Object
Dim RelDate As Range
Dim lastRow As Long
Dim dateCell, dateCell1 As Date
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
lastRow = Range("A" & Rows.Count).End(xlUp).Row
On Error GoTo cleanup
For Each RelDate In Range("K2:K" & lastRow)
If RelDate = "" Then GoTo 1
dateCell = RelDate.Value
dateCell1 = Cells(RelDate.Row, "I").Value
SendMailCell = Cells(RelDate.Row, "M").Value
If SendMailCell = "YES" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Cells(RelDate.Row, "AD").Value & ";" & Cells(RelDate.Row, "AE").Value & ";" & Cells(RelDate.Row, "AF").Value
.subject = "A new submission deadline set"
.body = "Dear " & Cells(RelDate.Row, "R").Value & "," _
& vbNewLine & vbNewLine & _
"A new submission deadline for " & Cells(RelDate.Row, "G").Value & _
" is now set to " & dateCell & " by " & Cells(RelDate.Row, "L").Value _
& vbNewLine & vbNewLine _
& vbNewLine & vbNewLine & _
"Regards," & vbNewLine & _
"XYZ"
.send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
1: Next RelDate
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
一切正常,但我遇到的问题是,如果M列中有多个“是”,则会发送多封电子邮件。我希望代码中有一行可以在每封电子邮件后清除M列的内容。
换句话说 - 我写了YES,电子邮件被发送,YES消失了。我在下一行输入了YES,发送了一封电子邮件,这个YES也消失了。
我尝试了所有Clear.Contents,Delete.Contents和所有这些,但似乎没有任何工作。它要么崩溃,要么一直从M上删除所有列。
提前谢谢大家!
答案 0 :(得分:0)
可能是您收到错误,因为您没有引用工作簿和工作表。如果列M位于工作簿的第一个工作表中,请尝试这样:
thisworkbook.sheets(1).Columns("M:M").ClearContents
答案 1 :(得分:0)
在.send之后和
之前添加此指令Cells(RelDate.Row, "M").Value=""