基于 Marc 提到的内容,这是一个久经考验的版本。
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,因为它将整个参数字符串限制为 250 个字符,而我需要大约 2000 个字符来发送消息。但看起来 SE 是在我的情况下真正可行的唯一选择。– Humanoid1000 7 小时前
这是我在下面的评论中提到的“可怕(我喜欢 JFC 所说的方式!!!)”方式,效果很好:) 顺便说一句,我只有 Outlook 作为我的默认客户端,所以我已经用它进行了测试。
代码
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
快照
包含消息的文本文件
发送前的电子邮件