2014-01-22 54 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