更改电子表格时,使用Outlook从Excel发送自动电子邮件

我有问题让Outlook通过Excel电子表格发送电子邮件。 当我在Outlook中使用Outlook 2013时没有任何问题,但是当我使用具有唯一域的先前版本(2010)(someEmail@CreatedDomain.mil)时,出现错误。 我想说的是因为领域,但我不确定。 以下是我在网上find的代码。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _ Cancel As Boolean) '-----AUTOMATIC EMAIL GENERATION----------------------- Dim answer As String answer = MsgBox("Saving this document will send an email to the database director and close the database. Are you sure you want to save?", _ vbYesNo, "Save confirmation") ' Above code informs the user ' that an automated email will be sent to the database ' director and prompts them if they are ready to save 'Code uses the users answer to either carryout the generated email process or to not save the changes. If answer = vbNo Then Cancel = True If Cancel = True Then Exit Sub If answer = vbYes Then 'Connects to outlook and retrieves information needed to create and send the email. Set OutlookApp = CreateObject("Outlook.Application") Set OlObjects = OutlookApp.GetNamespace("MAPI") Set newmsg = OutlookApp.CreateItem(olMailItem) 'Contains the email address of the person receiving the email. newmsg.Recipients.Add ("roman.hope.ctr@navy.mil") newmsg.Subject = "Automated email from excel database" 'Sets the automated subject line to the email newmsg.Body = "There has been a change to the excel database from the person sending linked to this email." 'Above code has the body of the automated email 'sets the email to "display" in the background then sends it in the next line of code (all in the background). newmsg.Display newmsg.Send 'sends the email 'Displays a confirmation that the email has been sent. MsgBox "The email has been successfully sent", , "Email confirmation" ' ActiveWorkbook.Save End If End Sub ' End of function 

对于Outlook 2010,我总是使用这种语法,就像一个魅力,甚至不需要命名空间。

 Dim OutApp As Object Dim OutMail As Object Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) On Error Resume Next With OutMail .To = "somebody@something.com" .CC = "" .BCC = "" .Subject = "YOUR SUBJECT" .HTMLBody = "your text as html" '.Attachments.Add ("path\filema.xsl") 'uncomment for attachment '.Display 'uncomment for display .Send End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing 

我还没有用Outlook 2013试过这个,但是到了2010年,这个电子邮件就发送了。 如果你有多个邮件accs在你的Outlook一些调整select正确的可能是必需的。 它应该工作,但是因为CreateItem(0)通常总是一个新的邮件,除非微软没有按预期工作。

您也可以将.HTMLBody更改为.Body,它应该可以工作,我只是喜欢HTML格式的邮件,因为那样您就可以轻松地在主体中使用附件。

我已经想出了这个问题,这不是在代码中,而是在我所在的networking上。

该networking具有自动电子邮件保护,阻止所有应用程序发送自动通用电子邮件。

感谢所有的帮助。