2014-01-22 61 views
0

目前我有一張表格可以將電子郵件發送到特定的電子郵件地址,在此表格中有一個具有兩個選項的特定驗證列表。如果我選擇一個選項,它將發送一封電子郵件到指定的電子郵件。但是,如果我選擇第二個選項沒有任何反應。沒有錯誤。excel vba發送電子郵件驗證列表

我希望能夠發送表單兩個不同的電子郵件地址,具體取決於列表中選擇的內容,然後按一下發送按鈕。

代碼:

Private Sub CommandButton1_Click() 

If Sheet1.Range("G31") = "in the cell(see notes below)" Then 

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(1) 
Dim fName As String 

fName = " NIFU - " & ws.Range("Q12") & " " & Format(Now, "ddmmyyyy hhmmss") & ".xls" 

ThisWorkbook.SaveAs fPath & fName, xlWorkbookNormal 

' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, Outlook 2010. 
' This example sends the last saved version of the Activeworkbook object . 
    Dim OutApp As Object 
    Dim OutMail As Object 

    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 

    On Error Resume Next 
    ' Change the mail address and subject in the macro before you run it. 
    With OutMail 
     .To = "[email protected] " 
     .CC = "" 
     .BCC = "" 
     .Subject = "RESTRICTED:" 
     .Body = "Hello," & vbNewLine & vbNewLine 
     .Attachments.Add ActiveWorkbook.FullName 
     ' You can add other files by uncommenting the following line. 
     '.Attachments.Add ("C:\test.txt") 
     ' In place of the following statement, you can use ".Display" to 
     ' display the mail. 
     .Send 
    End With 
    On Error GoTo 0 

    Set OutMail = Nothing 
    Set OutApp = Nothing 


    MsgBox "Thank you, this referral has been sucessfully sent" 

    Else 

    If Sheet1.Range("G31") = "Multiple applicants registered at the same address" Then 

    ' Change the mail address and subject in the macro before you run it. 
    With OutMail 
     .To = "[email protected]__________ " 
     .CC = "" 
     .BCC = "" 
     .Subject = "RESTRICTED:" 
     .Body = "Hello," & vbNewLine & vbNewLine 
     .Attachments.Add ActiveWorkbook.FullName 
     ' You can add other files by uncommenting the following line. 
     '.Attachments.Add ("C:\test.txt") 
     ' In place of the following statement, you can use ".Display" to 
     ' display the mail. 
     .Send 
    End With 
    On Error GoTo 0 

    Set OutMail = Nothing 
    Set OutApp = Nothing 

    MsgBox "Thank you, this referral has been sucessfully sent" 

      End If 
    End If 
End Sub 

回答

0

我已經初始化觀變量朝着如果語句外,它:

dim OutApp as object 
Set OutApp = CreateObject("Outlook.Application") 
dim OutMail as object 
set OutMail = OutApp.CreateItem(0) 

試試這個代碼出似乎工作。

私人小組CommandButton1_Click()

暗淡OutApp作爲對象 集OutApp =的CreateObject( 「Outlook.Application」) 暗淡發件作爲對象 組發件= OutApp.CreateItem(0)

如果Sheet 1中.Range( 「G31」)= 「在細胞中(參見下面的註釋)」 接着

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(1) 
Dim fName As String 

fName = " NIFU - " & ws.Range("Q12") & " " & Format(Now, "ddmmyyyy hhmmss") & ".xls" 

ThisWorkbook.SaveAs fPath & fName, xlWorkbookNormal 

' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, Outlook 2010. 
' This example sends the last saved version of the Activeworkbook object . 
Dim OutApp As Object 
Dim OutMail As Object 

Set OutApp = CreateObject("Outlook.Application") 
Set OutMail = OutApp.CreateItem(0) 

On Error Resume Next 
' Change the mail address and subject in the macro before you run it. 
With OutMail 
    .To = "[email protected]" 
    .CC = "" 
    .BCC = "" 
    .Subject = "RESTRICTED:" 
    .Body = "Hello," & vbNewLine & vbNewLine 
    .Attachments.Add ActiveWorkbook.FullName 
    ' You can add other files by uncommenting the following line. 
    '.Attachments.Add ("C:\test.txt") 
    ' In place of the following statement, you can use ".Display" to 
    ' display the mail. 
    .Send 
End With 
On Error GoTo 0 

Set OutMail = Nothing 
Set OutApp = Nothing 


MsgBox "Thank you, this referral has been sucessfully sent" 

elseif的Sheet1.Range( 「G31」)= 「在相同的地址註冊的多個申請人」 接着

' Change the mail address and subject in the macro before you run it. 
With OutMail 
    .To = "[email protected]__________ " 
    .CC = "" 
    .BCC = "" 
    .Subject = "RESTRICTED:" 
    .Body = "Hello," & vbNewLine & vbNewLine 
    .Attachments.Add ActiveWorkbook.FullName 
    ' You can add other files by uncommenting the following line. 
    '.Attachments.Add ("C:\test.txt") 
    ' In place of the following statement, you can use ".Display" to 
    ' display the mail. 
    .Send 
End With 
On Error GoTo 0 

Set OutMail = Nothing 
Set OutApp = Nothing 

MsgBox "Thank you, this referral has been sucessfully sent" 

結束如果結束 小組

0

我想通了,爲什麼它根本不工作。您需要在IF的兩個分支內聲明和設置對象。現在它的設置方式是,將它們聲明在頂層,但不是底層。

您必須在Else部分的生產線以及:現在

Private Sub CommandButton1_Click() 

If Sheet1.Range("G31") = "in the cell(see notes below)" Then 

    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(1) 
    Dim fName As String 

    fName = " NIFU - " & ws.Range("Q12") & " " & Format(Now, "ddmmyyyy hhmmss") & ".xls" 

    ThisWorkbook.SaveAs fPath & fName, xlWorkbookNormal 

    ' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, Outlook 2010. 
    ' This example sends the last saved version of the Activeworkbook object . 
    Dim OutApp As Object 
    Dim OutMail As Object 

    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 

    On Error Resume Next 
    ' Change the mail address and subject in the macro before you run it. 
    With OutMail 
     .To = "[email protected] " 
     .CC = "" 
     .BCC = "" 
     .Subject = "RESTRICTED:" 
     .Body = "Hello," & vbNewLine & vbNewLine 
     .Attachments.Add ActiveWorkbook.FullName 
     ' You can add other files by uncommenting the following line. 
     '.Attachments.Add ("C:\test.txt") 
     ' In place of the following statement, you can use ".Display" to 
     ' display the mail. 
     .Send 
    End With 
    On Error GoTo 0 

    Set OutMail = Nothing 
    Set OutApp = Nothing 


    MsgBox "Thank you, this referral has been sucessfully sent" 

ElseIf Sheet1.Range("G31") = "Multiple applicants registered at the same address" Then 

    Dim OutApp As Object 
    Dim OutMail As Object 

    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 

    ' Change the mail address and subject in the macro before you run it. 
    With OutMail 
     .To = "[email protected]__________ " 
     .CC = "" 
     .BCC = "" 
     .Subject = "RESTRICTED:" 
     .Body = "Hello," & vbNewLine & vbNewLine 
     .Attachments.Add ActiveWorkbook.FullName 
     ' You can add other files by uncommenting the following line. 
     '.Attachments.Add ("C:\test.txt") 
     ' In place of the following statement, you can use ".Display" to 
     ' display the mail. 
     .Send 
    End With 
    On Error GoTo 0 

    Set OutMail = Nothing 
    Set OutApp = Nothing 

    MsgBox "Thank you, this referral has been sucessfully sent" 

End If 
End Sub 
相關問題