跟进使用VBA发送电子邮件

这是关于使用VBA发送电子邮件的几条消息的后续步骤。

大多数build议使用Outlook,CDO或MAPI:

Set appOL = CreateObject("Outlook.Application") Set msgOne = CreateObject("CDO.Message") Set mapi_session = New MSMAPI.MAPISession 

但显然,Outlook将要求我改变我们的工作组安全设置,CDO和MAPI将要求我添加一个DLL或其他东西。

我正在尝试使用Excel在工作中组织小组作业,而且我无法以任何方式修改其他人的计算机。

有没有更简单的方法发送电子邮件从Excelmacros?
我所需要的只是邮件正文中的一个文本块,没有附件。

我一整个星期都在Google,MSDN和StackOverflow上耕耘,我被困在一艘慢船上。

build立在马克提到的,这是一个久经考验的版本。

 Option Explicit Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _ ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _ ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long Sub SendMail() Dim objMail As String Dim oMailSubj, oMailTo, oMailBody As String On Error GoTo Whoa oMailSubj = "YOUR SUBJECT GOES HERE" oMailTo = "ABC@ABC.COM" oMailBody = "BLAH BLAH!!!!" objMail = "mailto:" & oMailTo & "?subject=" & oMailSubj & "&body=" & oMailBody ShellExecute 0, vbNullString, objMail, vbNullString, vbNullString, vbNormalFocus Application.Wait (Now + TimeValue("0:00:03")) Application.SendKeys "%s" Exit Sub Whoa: MsgBox Err.Description End Sub 

跟进

谢谢(你的)信息。 我已经排除了ShellExecute因为它限制了整个参数string为250个字符,我需要大约2000年的消息。 但看起来SE是唯一的select,实际上在我的情况下工作。 – 7小时前的Humanoid1000

这里是“可怕的( 我爱的方式JFC说!!! )”的方式,我在下面的评论中提到的工作很好:)顺便说一句我只有Outlook作为我的默认客户端,所以我testing了它。

 Option Explicit Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _ ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _ ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Sub SendMail() Dim objMail As String Dim oMailSubj As String, oMailTo As String Dim i As Long Dim objDoc As Object, objSel As Object, objOutlook As Object Dim MyData As String, strData() As String On Error GoTo Whoa '~~> Open the txt file which has the body text and read it in one go Open "C:\Users\Siddharth Rout\Desktop\Sample.Txt" For Binary As #1 MyData = Space$(LOF(1)) Get #1, , MyData Close #1 strData() = Split(MyData, vbCrLf) Sleep 300 oMailSubj = "YOUR SUBJECT GOES HERE" oMailTo = "ABC@ABC.COM" objMail = "mailto:" & oMailTo & "?subject=" & oMailSubj ShellExecute 0, vbNullString, objMail, vbNullString, vbNullString, vbNormalFocus Sleep 300 Set objOutlook = GetObject(, "Outlook.Application") '~~> Get a Word.Selection from the open Outlook item Set objDoc = objOutlook.ActiveInspector.WordEditor Set objSel = objDoc.Windows(1).Selection objDoc.Activate Sleep 300 For i = LBound(strData) To UBound(strData) objSel.TypeText strData(i) objSel.TypeText vbNewLine Next i Set objDoc = Nothing Set objSel = Nothing '~~> Uncomment the below to actually send the email 'Application.Wait (Now + TimeValue("0:00:03")) 'Application.SendKeys "%s" Exit Sub Whoa: MsgBox Err.Description End Sub 

快照

包含消息的文本文件

在这里输入图像说明

电子邮件发送之前

在这里输入图像说明

您可以使用Shell命令。

 Shell("mailto:username@isp.com") 

如果Outlook是您的默认电子邮件程序,我认为Windows会正确解释(这是从Windows的运行对话框,但不知道如果从VBA)。 试试看。 我目前不在Excel之前。

查看“mailto”的参数,了解如何将主题和主体添加到该string。

更正:这将只会生成电子邮件,但不会发送它。 不理我的答案。

这是最后的结果:

事实certificate, SendMail()在家工作,但不工作,但CreateObject("Outlook.Application")确实在工作。

我的安全设置不是Outlook的问题,这是一个世俗的错字。

无论哪种方式,这个网页上有很多关于这个话题的人都很好的信息。