2013-04-18 30 views
0

我試圖通過從C.Pearson下載的函數發送電子郵件。當我嘗試使它工作時,我得到錯誤:「你輸入的表達式有一個包含錯誤數量參數的函數。」使用AutoExec發送電子郵件時參數錯誤

這裏是我目前使用的代碼:

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", "[email protected]", "[email protected]","Hello", SMTP_Server:="smtp.gmail.com", "D:\Other\modemail.bas", Attachments:=Empty) 

End Sub 

我有一個名爲「自動執行」宏,這個代碼是一個名爲「modEmail」模塊中。

我的宏的動作是RunCode和功能名稱是:「SendEMail()」

我怎樣才能解決這個問題?我一直在玩這個代碼幾天,我似乎無法想出一個解決方案。

注意:我的端口25被我的ISP阻塞,會導致這個特定的錯誤?

在此先感謝您的幫助。 :)

+0

檢查你是如何調用它 - 只要使用'sendmail的()'將無法正常工作,你將有數據填充未列出的所有參數作爲可選的例如'sendmail(「MySubject」,「[email protected]」,「[email protected]」,「組成電子郵件的文本」,「mysmtpserveraddress」) – SeanC

回答

1

正如你可以從VBA代碼中Function聲明看,該函數期望你提供主題行中,FROMADDRESS的的toAddress等作爲參數。如果你的宏只是簡單地調用SendEMail()(沒有任何參數),那麼這就是問題所在,因爲你沒有告訴函數要發送什麼或給誰發送。

函數調用需要的東西更像

SendEMail("this is the Subject", "[email protected]", "[email protected]", ... 
+0

好吧我編輯我的問題與代碼我用它,它給我編譯錯誤:'期望:命名參數'的BodyFileName部分? –