我正在尝试通过我从C.Pearson下载的功能发送电子邮件。当我尝试使其工作时,我得到错误:“您输入的表达式包含一个包含错误数量的参数的函数。”
这是我目前正在使用的代码:
Option Explicit
Option Compare Text
Function SendEMail(Subject As String, _
FromAddress As String, _
ToAddress As String, _
MailBody As String, _
SMTP_Server As String, _
Optional BodyFileName As String, _
Optional Attachments As Variant = Empty) As Boolean
Dim MailMessage As CDO.Message
Dim N As Long
Dim FNum As Integer
Dim S As String
Dim Body As String
Dim Recips() As String
Dim Recip As String
Dim NRecip As Long
' ensure required parameters are present and valid.
If Len(Trim(Subject)) = 0 Then
SendEMail = False
Exit Function
End If
If Len(Trim(FromAddress)) = 0 Then
SendEMail = False
Exit Function
End If
If Len(Trim(SMTP_Server)) = 0 Then
SendEMail = False
Exit Function
End If
' Clean up the addresses
Recip = Replace(ToAddress, Space(1), vbNullString)
Recips = Split(Recip, ";")
For NRecip = LBound(Recips) To UBound(Recips)
On Error Resume Next
' Create a CDO Message object.
Set MailMessage = CreateObject("CDO.Message")
If Err.Number <> 0 Then
SendEMail = False
Exit Function
End If
Err.Clear
On Error GoTo 0
With MailMessage
.Subject = Subject
.From = FromAddress
.To = Recips(NRecip)
If MailBody <> vbNullString Then
.TextBody = MailBody
Else
If BodyFileName <> vbNullString Then
If Dir(BodyFileName, vbNormal) <> vbNullString Then
' import the text of the body from file BodyFileName
FNum = FreeFile
S = vbNullString
Body = vbNullString
Open BodyFileName For Input Access Read As #FNum
Do Until EOF(FNum)
Line Input #FNum, S
Body = Body & vbNewLine & S
Loop
Close #FNum
.TextBody = Body
Else
' BodyFileName not found.
SendEMail = False
Exit Function
End If
End If ' MailBody and BodyFileName are both vbNullString.
End If
If IsArray(Attachments) = True Then
' attach all the files in the array.
For N = LBound(Attachments) To UBound(Attachments)
' ensure the attachment file exists and attach it.
If Attachments(N) <> vbNullString Then
If Dir(Attachments(N), vbNormal) <> vbNullString Then
.AddAttachment Attachments(N)
End If
End If
Next N
Else
' ensure the file exists and if so, attach it to the message.
If Attachments <> vbNullString Then
If Dir(CStr(Attachments), vbNormal) <> vbNullString Then
.AddAttachment Attachments
End If
End If
End If
With .Configuration.Fields
' set up the SMTP configuration
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTP_Server
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
On Error Resume Next
Err.Clear
' Send the message
.Send
If Err.Number = 0 Then
SendEMail = True
Else
SendEMail = False
Exit Function
End If
End With
Next NRecip
End Function
Public Sub EmailSendSub()
SendEMail ("My Email", "spetsnaz307@gmail.com", "spetsnaz307@hotmail.co.nz","Hello", SMTP_Server:="smtp.gmail.com", "D:\Other\modemail.bas", Attachments:=Empty)
End Sub
我有一个名为'autoexec'的宏,此代码位于名为'modEmail'的模块中。
我的宏的动作是RunCode,函数名是:'SendEMail()'
如何修复此错误?我已经玩了几天这个代码,我似乎无法想出一个解决方案。
注意:我的端口25被我的ISP阻止,会导致此特定错误吗?
提前感谢您的帮助。 :)
答案 0 :(得分:1)
从VBA代码中的Function
声明中可以看出,该函数要求您提供Subject行,FromAddress,ToAddress等作为参数。如果您的宏只是调用SendEMail()
(没有任何参数)那么这就是问题所在,因为您没有告诉函数要发送什么,或者向谁发送。
函数调用需要更像
SendEMail("this is the Subject", "sender@example.com", "recipient@example.com", ...