由于添加了抄送功能,因此无法使用Access中的表单发送电子邮件。我该如何解决?

时间:2019-04-15 02:01:44

标签: vba ms-access

我在代码中包含了cc函数,并且可以正常工作。我能够发送电子邮件并抄送其他人。但是,如果不填写“抄送”字段,则无法发送电子邮件。仅当我填写“收件人”和“抄送”字段时,电子邮件才起作用。我该如何解决?

Private Sub btnBrowse_Click()
    Dim fileDiag As FileDialog
    Dim file As Variant

    Set fileDiag = FileDialog(msoFileDialogFilePicker)

    fileDiag.AllowMultiSelect = False
    If fileDiag.Show Then
        For Each file In fileDiag.SelectedItems
            Me.txtAttachment = file
        Next
    End If
End Sub

Private Sub btnClear_Click()
    Me.txtBody = Null
    Me.txtSubject = Null
    Me.txtTo = Null
    Me.txtAttachment = Null
    Me.txtCC = Null

End Sub

Private Sub btnHome_Click()
    DoCmd.BrowseTo 2, "HomePageMainFrm"
End Sub

Private Sub btnSend_Click()
    Dim oApp As Object
    Dim oEmail As Object

    Set oApp = CreateObject("outlook.Application")
    Set oEmail = oApp.CreateItem(0)

    oEmail.To = Me.txtTo.Value
    oEmail.Subject = Me.txtSubject.Value
    oEmail.CC Me.txtCC.Value
    oEmail.Body = Me.txtBody.Value
    If Len(Me.txtAttachment) > 0 Then
        oEmail.Attachments.Add Me.txtAttachment.Value
    End If
    With oEmail
        If Not IsNull(.To) And Not IsNull(.Subject) And Not IsNull(.Body) And Not IsNull(.CC) Then
            .Send
            MsgBox "Email Sent!"
        Else
            MsgBox "Please fill out the required fields."
        End If
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

CC对象的MailItem属性是一个字符串值的属性,如果没有收件人,则其值为空字符串("")-此属性不应为空。

您可以使用Nz函数在代码中轻松解决此问题,如果提供的参数为null,该函数将返回一个替代值:

oEmail.CC = Nz(Me.txtCC.Value, "")

但是,如上面链接的MS VBA文档中所述,CC属性仅包含一个用分号分隔的显示名称列表-添加时,您应该修改Recipients集合的内容收件人(与收件人类型无关)到MailItem对象。

使用Recipients方法将一个项目添加到Add集合中将产生一个Recipient对象,该对象具有一个Type属性,可用于将接收者指定为通过将属性设置为olCC(如果使用后期绑定,则设置为2)来抄送。