【问题标题】:Outlook VBA Recipient emailOutlook VBA 收件人电子邮件
【发布时间】:2020-06-25 21:51:59
【问题描述】:

我正在尝试获取收件人电子邮件地址并插入 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

【问题讨论】:

  • 我没有看到你在哪里声明/初始化了 Mail 对象。应该是Set recips = Item.Recipients

标签: vba outlook


【解决方案1】:
  1. “邮件”仅在您的 Sub 中出现一个。它是一个全局(pfui)变量吗?您确定在调用Application_ItemSend 之前它已正确初始化吗? [添加:您在评论中链接到的代码 sn-p 接收一个名为 mail 的 Outlook.MailItem 作为参数;这使得@simoco 的建议尝试Set recips = Item.Recipients(您的参数名称中的项目)很有希望;当然,只有当您的调用代码正确初始化该参数时,它才会起作用。]
  2. 你昏暗并使用recip,但我看不出你在哪里为它赋值。

【讨论】:

猜你喜欢
  • 2023-03-22
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多