1

嗨,我正在 VB 中开发一个小型应用程序,用于从存储在 Access 数据库中的电子邮件地址列表中发送单独的邮件。我正在使用 ADODC 控制器连接 VB 和 Access。但是在循环通过 ADODC 控制器时,我收到错误“项目已被移动或删除”。你们能帮我解决这个问题吗?下面是我正在使用的代码。我想为每个地址发送单独的邮件,所以不能使用.Recipients.Add命令。

Private Sub Send_Click()
  Dim oOApp As Outlook.Application
  Dim oOMail As Outlook.MailItem

  Set oOApp = CreateObject("Outlook.Application")
  Set oOMail = oOApp.CreateItem(olMailItem)

  With oOMail

    Adodc1.Recordset.MoveFirst

    While Adodc1.Recordset.EOF = False
     .To = Text1.Text   <------ getting error in this line in second iteration
     .Subject = Subject.Text
     .Body = MsgBody.Text
     If path1.Text <> "" Then
       .Attachments.Add path1.Text, olByValue, 1
     End If
    .Send
    Adodc1.Recordset.MoveNext
  Wend
End Sub
4

2 回答 2

4

.Send 将发送电子邮件。在第一次迭代中,电子邮件将被发送并且 oOMail 将丢失。在第一个循环之后,您将开始收到您提到的错误。

编辑 - - - - - - - - - - -

很抱歉没有早点重写代码。假设您必须在一封电子邮件中添加所有附件然后发送

编辑 - - - - - - - - - - - - - - - - - - -

如果您想每次都创建电子邮件对象

Private Sub Send_Click()
Dim oOApp As Outlook.Application
Dim oOMail As Outlook.MailItem

Set oOApp = CreateObject("Outlook.Application")




Adodc1.Recordset.MoveFirst

While Adodc1.Recordset.EOF = False
 Set oOMail = oOApp.CreateItem(olMailItem)
 With oOMail
.To = Text1.Text   <------ getting error in this line in second iteration
.Subject = Subject.Text
.Body = MsgBody.Text
If path1.Text <> "" Then
.Attachments.Add path1.Text, olByValue, 1
End If

Adodc1.Recordset.MoveNext
Wend

.Send   'Sending the email in the end

结束子

于 2012-08-13T17:19:51.273 回答
1

您的代码是正确的,您只需将 with 放入循环而不是相反

Private Sub Send_Click()
    Dim oOApp As Outlook.Application
    Dim oOMail As Outlook.MailItem
    Set oOApp = CreateObject("Outlook.Application")
    Set oOMail = oOApp.CreateItem(olMailItem)
    Adodc1.Recordset.MoveFirst
    While Adodc1.Recordset.EOF = False
        With oOMail
            .To = Text1.Text  
            .Subject = Subject.Text
            .Body = MsgBody.Text
            If path1.Text <> "" Then
                .Attachments.Add path1.Text, olByValue, 1
            End If
            .save
            .send
        End with.
        Adodc1.Recordset.MoveNext
    Wend
End sub
于 2014-02-20T06:44:53.930 回答