我在让Outlook通过Excel电子表格发送电子邮件时遇到问题。当我将Outlook 2013与Gmail一起使用时没有问题,但是当我使用具有唯一域([email protected])的早期版本(2010)时,出现了错误。我想说的是因为领域,但我不确定。以下是我在网上找到的代码。
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 ("[email protected]")
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 = "[email protected]"
.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年中,它会按原样发送电子邮件。如果您的Outlook中有多个邮件帐户,则可能需要对选择正确的邮件帐户进行一些调整。但是它应该起作用,因为除非Microsoft无法按预期工作,否则CreateItem(0)通常通常是一封新邮件。
您还可以将.HTMLBody更改为.Body,它应该可以工作,我只喜欢将html格式设置为邮件,因为这样您就可以轻松地在正文中使用附件了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句