【发布时间】: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?