Outlook VBA收件人电子邮件

时间:2014-04-01 15:26:36

标签: outlook-vba

我正在尝试接收收件人的电子邮件地址并插入到SQL数据库中但是我得到了运行时错误424对象,

以下以黄色“Set recips = Mail.Recipients”

突出显示

我不知道我做错了什么任何帮助将不胜感激

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim Atmt As attachment
Dim FileName As String
Dim i As Integer
Dim strPrompt As String
Dim vError As Variant
Dim sErrors As String

i = 0

For Each Atmt In Item.Attachments
Debug.Print Atmt.FileName

If (UCase(Right(Atmt.FileName, 4)) = UCase("docx")) Or _
   (UCase(Right(Atmt.FileName, 3)) = UCase("pdf")) Or _
   (UCase(Right(Atmt.FileName, 3)) = UCase("doc")) Then

i = i + 1

End If


Next Atmt

    If i > 0 Then

    strPrompt = "You have attached a document. Is this a CV Submission?"

        If MsgBox(strPrompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Attachment") = vbNo Then
            Cancel = False

        Else:

              Dim myNamespace As Outlook.NameSpace
              Dim recip As Outlook.Recipient
              Dim recips As Outlook.Recipients
              Dim conn As ADODB.Connection
              Dim rs As ADODB.Recordset
              Dim sConnString As String


              ' Create the connection string.
              sConnString = "Provider=SQLOLEDB;Data Source=WIN-NBST3PHVFV4\ECLIPSE;" & _
                            "Initial Catalog=OBlive;" & _
                            "User ID=outlook;Password=0Zzy007;"

              ' Create the Connection and Recordset objects.
             Set conn = New ADODB.Connection
             Set rs = New ADODB.Recordset
             Set myNamespace = Application.GetNamespace("MAPI")
             Set recips = Mail.Recipients

             ' Open the connection and execute.
             conn.Open sConnString
             Set rs = conn.Execute("INSERT INTO dbo.Submissions (CV_Sent, Consultant, Timestamp, Recipient) VALUES ( '1','" & myNamespace.CurrentUser & "', CURRENT_TIMESTAMP, '" & recip.Address & "' )")
             ' Clean up
             If CBool(conn.State And adStateOpen) Then conn.Close
             Set conn = Nothing
             Set rs = Nothing

        End If

    End If

End Sub

1 个答案:

答案 0 :(得分:0)

  1. '邮件'只出现在您的Sub中。它是一个全局(pfui)变量吗?在你致电Application_ItemSend之前,你确定它已经正确初始化了吗? [添加:您在评论中链接到的代码段收到名为mail的Outlook.MailItem作为参数;这使@ simoco的建议尝试Set recips = Item.Recipients(项目为你的参数名称)有希望;当然,只有在您的调用代码正确地启动了该参数时,它才会起作用。]
  2. 你昏暗并使用recip,但我看不到你为它指定值的位置。