从Excel发送电子邮件警报到Outlook

我想从Excel发送testing邮件到Outlook,但是我收到错误消息:运行时错误287在以下行中:

OutMail.Send 

请在下面find我的代码:

 Sub sendds() Dim OutMail As MailItem Dim outlookapp As Outlook.Application Dim myInspector As Outlook.Inspector Set outlookapp = CreateObject("Outlook.application") Set OutMail = outlookapp.CreateItem(olMailItem) With OutMail .To = "email address" .Subject = "test mail" .Body = "Hi this is test email" OutMail.Send 'Getting error on this line End With Set outlookapp = Nothing Set OutMail = Nothing End Sub 

这是因为你有不正确的电子邮件或电子邮件地址格式应该是email@email.com或用于testing目的使用.Display

也改变它只是。 .Send

  With OutMail .To = "email@address.com" .Subject = "test" .Body = "Hi this is test email" .Send End With 

**解决方法**

  With olMail .To = "email" .CC = "" .BCC = "" .Subject = "" .Display .Send End With 

尝试下面的方法:

 Public Sub emailUsFeature() Set outApp = CreateObject("Outlook.Application") Set outMail = outApp.CreateItem(olMailItem) With outMail .To = "abc@xyz.com; def@xyz.com" .CC = "ghi@xyz.com" .BCC = "jkl@xyz.com" .Subject = "This is the subject." End With outMail.display End Sub 

根据评论“当我使用outMail.display它显示我想要发送的电子邮件,但我真的要发送电子邮件”代码太快。 如果你joinF8,这也可能会起作用。

您可以使用Excel的等待来延迟发送。

这也适用于所有的应用程序,这将是最短的等待期。

 Sub sendds_ErrorHandlerWait() Dim OutMail As MailItem Dim outlookapp As Outlook.Application Dim myInspector As Outlook.Inspector Set outlookapp = CreateObject("Outlook.application") Set OutMail = outlookapp.CreateItem(olMailItem) With OutMail .To = "email address" .Subject = "test mail" .body = "Hi this is test email" On Error GoTo ErrorHandler ' Err.Raise 287 ' for testing ' Err.Raise 1 ' for testing .Send On Error GoTo 0 End With ExitRoutine: Set outlookapp = Nothing Set OutMail = Nothing Exit Sub ErrorHandler: Select Case Err Case 287 DoEvents ' To accept clicks and to allow escaping if Outlook never opens Debug.Print " <Ctrl> + <Break> to escape" Resume Case Else On Error GoTo 0 ' Break on other lines with an error Resume End Select End Sub 

看来你的Outlook安装程序需要显示。 如果没有解决这种情况,你可以使用隐形显示器。

 Sub sendds_InspectorRatherThanDisplay() Dim OutMail As mailItem Dim outlookapp As Outlook.Application Dim myInspector As Outlook.Inspector Set outlookapp = CreateObject("Outlook.application") Set OutMail = outlookapp.CreateItem(olMailItem) With OutMail .To = "email address" .Subject = "test mail" .body = "Hi this is test email" Set myInspector = .GetInspector .Send End With ExitRoutine: Set outlookapp = Nothing Set OutMail = Nothing Set myInspector = Nothing End Sub