Excel到Outlook:。发送返回运行时错误
这是以前问过的问题,但是,我已经测试了之前提供的所有解决方案,没有一个有效,所以我在这里问 我有excel代码,如果满足一组特定的条件,它会发送电子邮件。我已经三次检查了我的数据,那里没有错误。几天前代码运行良好,突然我收到一个运行时错误“287”:应用程序定义的错误或对象定义的错误。在Excel到Outlook:。发送返回运行时错误,excel,vba,outlook,runtime-error,Excel,Vba,Outlook,Runtime Error,这是以前问过的问题,但是,我已经测试了之前提供的所有解决方案,没有一个有效,所以我在这里问 我有excel代码,如果满足一组特定的条件,它会发送电子邮件。我已经三次检查了我的数据,那里没有错误。几天前代码运行良好,突然我收到一个运行时错误“287”:应用程序定义的错误或对象定义的错误。在.Send行。当我改用.Display时,它工作正常,但显然我必须手动发送电子邮件 这是我的密码: Sub mailing() Dim OutApp As Object Dim OutMail
.Send
行。当我改用.Display
时,它工作正常,但显然我必须手动发送电子邮件
这是我的密码:
Sub mailing()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim lastrow As Long
Dim ws As Worksheet
lastrow = Worksheets("2018").Cells(Rows.Count, "Y").End(xlUp).Row
Dim rgRem1 As Range 'rg, Reminder 1
Dim rgRem2 As Range 'Reminder 2
Dim rgRem3 As Range 'Reminder 3
Dim rgAssigned As Range 'rg2, days since assigned
Set ws = Worksheets("2018")
With ws
lastrow = .Cells(Rows.Count, "G").End(xlUp).Row
Set rgAssigned = Range(.Cells(1, "X"), .Cells(lastrow, "X"))
Set rgRem1 = Range(.Cells(1, "Y"), .Cells(lastrow, "Y"))
Set rgRem2 = Range(.Cells(1, "z"), .Cells(lastrow, "z"))
Set rgRem3 = Range(.Cells(1, "aa"), .Cells(lastrow, "aa"))
End With
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
For Each cell In rgRem1
Set OutMail = OutApp.CreateItem(0)
If cell.Value = "SENDING" Then 'try with less conditions first
With OutMail
.To = Cells(cell.Row, "V").Value
.Subject = "WO# " & Cells(cell.Row, "G").Value & " - Reminder"
.Body = "Work Order: " & Cells(cell.Row, "G").Value & _
" has been assigned to you for " & Cells(cell.Row, "x").Value & " days and is not yet completed. Can you provide any updates?" & _
vbNewLine & vbNewLine & _
"Region: " & Cells(cell.Row, "B").Value & vbNewLine & _
"District: " & Cells(cell.Row, "C").Value & vbNewLine & _
"City: " & Cells(cell.Row, "D").Value & vbNewLine & _
"Atlas: " & Cells(cell.Row, "E").Value & vbNewLine & _
"Notification Number: " & Cells(cell.Row, "F").Value & vbNewLine
'.ReadReceiptRequested = True
.Send
End With
Cells(cell.Row, "Y").Value = Now
Set OutMail = Nothing
End If
Next cell
For Each cell In rgRem2
Set OutMail = OutApp.CreateItem(0)
If cell.Value = "SENDING" Then 'try with less conditions first
With OutMail
.To = Cells(cell.Row, "V").Value
.Subject = "WO# " & Cells(cell.Row, "G").Value & " - Reminder"
.Body = "Work Order: " & Cells(cell.Row, "G").Value & _
" has been assigned to you for " & Cells(cell.Row, "x").Value & " days and is not yet completed. Can you provide any updates?" & _
vbNewLine & vbNewLine & _
"Region: " & Cells(cell.Row, "B").Value & vbNewLine & _
"District: " & Cells(cell.Row, "C").Value & vbNewLine & _
"City: " & Cells(cell.Row, "D").Value & vbNewLine & _
"Atlas: " & Cells(cell.Row, "E").Value & vbNewLine & _
"Notification Number: " & Cells(cell.Row, "F").Value & vbNewLine
'.ReadReceiptRequested = True
.Send
End With
Cells(cell.Row, "z").Value = Now
Set OutMail = Nothing
End If
Next cell
For Each cell In rgRem3
Set OutMail = OutApp.CreateItem(0)
If cell.Value = "SENDING" Then 'try with less conditions first
With OutMail
.To = Cells(cell.Row, "V").Value
.Subject = "WO# " & Cells(cell.Row, "G").Value & " - Reminder"
.Body = "Work Order: " & Cells(cell.Row, "G").Value & _
" has been assigned to you for " & Cells(cell.Row, "x").Value & " days and is not yet completed. Can you provide any updates?" & _
vbNewLine & vbNewLine & _
"Region: " & Cells(cell.Row, "B").Value & vbNewLine & _
"District: " & Cells(cell.Row, "C").Value & vbNewLine & _
"City: " & Cells(cell.Row, "D").Value & vbNewLine & _
"Atlas: " & Cells(cell.Row, "E").Value & vbNewLine & _
"Notification Number: " & Cells(cell.Row, "F").Value & vbNewLine
'.ReadReceiptRequested = True
.Send
End With
Cells(cell.Row, "aa").Value = Now
Set OutMail = Nothing
End If
Next cell
'Set OutApp = Nothing 'it will be Nothing after End Sub
Application.ScreenUpdating = True
'For Each cell In rgAssigned
'If cell.Value = 25 And cell.Value <> "Completed" And cell.Value <> "Over 75 days since assigned" And cell.Value <> "" And Cells(cell.Row, "Y").Value = "" Then
'Cells(cell.Row, "Y").Value = "SENDING"
'End If
'If cell.Value = 50 And cell.Value <> "Completed" And cell.Value <> "Over 75 days since assigned" And cell.Value <> "" And Cells(cell.Row, "Z").Value = "" Then
'Cells(cell.Row, "Z").Value = "SENDING"
'End If
'If cell.Value = 75 And cell.Value <> "Completed" And cell.Value <> "Over 75 days since assigned" And cell.Value <> "" And Cells(cell.Row, "AA").Value = "" Then
'Cells(cell.Row, "AA").Value = "SENDING"
'End If
'Next cell
End Sub
子邮件()
Dim OutApp作为对象
将邮件变暗为对象
暗淡单元格作为范围
最后一排一样长
将ws设置为工作表
lastrow=工作表(“2018”)。单元格(Rows.Count,“Y”)。结束(xlUp)。行
变暗rgRem1为范围“rg”,提醒1
变暗rgRem2为范围“提醒2
变暗rgRem3为范围“提醒3
指定为范围“rg2”的Dim RGA,自分配后的天数
设置ws=工作表(“2018”)
与ws
lastrow=.Cells(Rows.Count,“G”).End(xlUp).Row
设置rgAssigned=Range(.Cells(1,“X”),.Cells(lastrow,“X”))
设置rgRem1=范围(.Cells(1,“Y”),.Cells(最后一行,“Y”))
设置rgRem2=范围(.Cells(1,“z”),.Cells(lastrow,“z”))
设置rgRem3=范围(.Cells(1,“aa”),.Cells(lastrow,“aa”))
以
Application.ScreenUpdating=False
Set-OutApp=CreateObject(“Outlook.Application”)
对于rgRem1中的每个单元格
Set-OutMail=OutApp.CreateItem(0)
如果cell.Value=“SENDING”,则“首先尝试使用较少的条件”
发邮件
.To=单元格(cell.Row,“V”).值
.Subject=“WO#”和单元格(cell.Row,“G”)。值和“-提醒”
.Body=“工单:”&单元格(cell.Row,“G”)。值_
“已为您分配了”&Cells(cell.Row,“x”).Value&“天,尚未完成。您能提供任何更新吗?”&_
vbNewLine&vbNewLine&_
“区域:”&单元格(cell.Row,“B”)。值&vbNewLine&_
“地区:”&单元格(cell.Row,“C”)。值和vbNewLine&_
“城市:”&单元格(cell.Row,“D”)。值和vbNewLine&_
地图集:“&Cells(cell.Row,“E”)。Value&vbNewLine&_
“通知编号:”&单元格(cell.Row,“F”)。值和vbNewLine
'.ReadReceiptRequested=True
.发送
以
单元格(cell.Row,“Y”)。值=现在
发送邮件=无
如果结束
下一个细胞
对于rgRem2中的每个单元格
Set-OutMail=OutApp.CreateItem(0)
如果cell.Value=“SENDING”,则“首先尝试使用较少的条件”
发邮件
.To=单元格(cell.Row,“V”).值
.Subject=“WO#”和单元格(cell.Row,“G”)。值和“-提醒”
.Body=“工单:”&单元格(cell.Row,“G”)。值_
“已为您分配了”&Cells(cell.Row,“x”).Value&“天,尚未完成。您能提供任何更新吗?”&_
vbNewLine&vbNewLine&_
“区域:”&单元格(cell.Row,“B”)。值&vbNewLine&_
“地区:”&单元格(cell.Row,“C”)。值和vbNewLine&_
“城市:”&单元格(cell.Row,“D”)。值和vbNewLine&_
地图集:“&Cells(cell.Row,“E”)。Value&vbNewLine&_
“通知编号:”&单元格(cell.Row,“F”)。值和vbNewLine
'.ReadReceiptRequested=True
.发送
以
单元格(cell.Row,“z”)。值=现在
发送邮件=无
如果结束
下一个细胞
对于rgRem3中的每个单元格
Set-OutMail=OutApp.CreateItem(0)
如果cell.Value=“SENDING”,则“首先尝试使用较少的条件”
发邮件
.To=单元格(cell.Row,“V”).值
.Subject=“WO#”和单元格(cell.Row,“G”)。值和“-提醒”
.Body=“工单:”&单元格(cell.Row,“G”)。值_
“已为您分配了”&Cells(cell.Row,“x”).Value&“天,尚未完成。您能提供任何更新吗?”&_
vbNewLine&vbNewLine&_
“区域:”&单元格(cell.Row,“B”)。值&vbNewLine&_
“地区:”&单元格(cell.Row,“C”)。值和vbNewLine&_
“城市:”&单元格(cell.Row,“D”)。值和vbNewLine&_
地图集:“&Cells(cell.Row,“E”)。Value&vbNewLine&_
“通知编号:”&单元格(cell.Row,“F”)。值和vbNewLine
'.ReadReceiptRequested=True
.发送
以
单元格(cell.Row,“aa”)。值=现在
发送邮件=无
如果结束
下一个细胞
“Set-OutApp=Nothing”结束Sub后将为Nothing
Application.ScreenUpdating=True
'对于rgAssigned中的每个单元格
'如果cell.Value=25,cell.Value“已完成”,cell.Value“自分配后超过75天”,cell.Value“”和cell(cell.Row,“Y”).Value=”“,则
'单元格(cell.Row,“Y”).Value=“发送”
"完"
'如果cell.Value=50,cell.Value“已完成”,cell.Value“自分配后超过75天”,cell.Value“”和cell(cell.Row,“Z”).Value=”“,则
'单元格(cell.Row,“Z”).Value=“发送”
"完"
'如果cell.Value=75,cell.Value“已完成”,cell.Value“自分配后超过75天”,cell.Value“”和cell(cell.Row,“AA”).Value=”“,则
'单元格(cell.Row,“AA”).Value=“发送”
"完"
“下一个细胞
端接头
我在网上尝试了几种建议的解决方案,包括:
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderDrafts).Items
Set objNS = Nothing
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
EmailOutlookDraftsMessages
End Sub
Public Sub EmailOutlookDraftsMessages()
Dim lDraftItem As Long
Dim myOutlook As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolders As Outlook.Folders
Dim myDraftsFolder As Outlook.MAPIFolder
Set myOutlook = Outlook.Application
Set myNameSpace = myOutlook.GetNamespace("MAPI")
Set myFolders = myNameSpace.Folders
Set myDraftsFolder = myFolders("insight@brickworks.com.au").Folders("Drafts")
For lDraftItem = myDraftsFolder.Items.Count To 1 Step -1
If Len(Trim(myDraftsFolder.Items.Item(lDraftItem).To)) > 0 Then
myDraftsFolder.Items.Item(lDraftItem).Send
End If
Next lDraftItem
Set myDraftsFolder = Nothing
Set myNameSpace = Nothing
Set myOutlook = Nothing
End Sub
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderDrafts).Items
Set objNS = Nothing
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
EmailOutlookDraftsMessages
End Sub
Public Sub EmailOutlookDraftsMessages()
Dim lDraftItem As Long
Dim myOutlook As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolders As Outlook.Folders
Dim myDraftsFolder As Outlook.MAPIFolder
Set myOutlook = Outlook.Application
Set myNameSpace = myOutlook.GetNamespace("MAPI")
Set myFolders = myNameSpace.Folders
Set myDraftsFolder = myFolders("R.E.L.s_email@fake.com").Folders("Drafts")
For lDraftItem = myDraftsFolder.Items.Count To 1 Step -1
If Len(Trim(myDraftsFolder.Items.Item(lDraftItem).To)) > 0 And InStr(myDraftsFolder.Items.Item(lDraftItem).Subject, "WO# ") > 0 Then
myDraftsFolder.Items.Item(lDraftItem).Send
End If
Next lDraftItem
Set myDraftsFolder = Nothing
Set myNameSpace = Nothing
Set myOutlook = Nothing
End Sub