发送带有附件的电子邮件..VBA代码

发送带有附件的电子邮件..VBA代码,vba,excel,Vba,Excel,我正在尝试发送带有附件的电子邮件: 我的代码: Sub SendEmailUsingGmail() Dim Text As String Dim Text2 As String Dim i As Integer Dim j As Integer Dim NewMail As CDO.Message Set NewMail = New CDO.Message NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/

我正在尝试发送带有附件的电子邮件:

我的代码:

Sub SendEmailUsingGmail()
Dim Text As String
Dim Text2 As String
Dim i As Integer
Dim j As Integer
Dim NewMail As CDO.Message

Set NewMail = New CDO.Message

NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

'Make SMTP authentication Enabled=true (1)

NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

'Set the SMTP server and port Details
'To get these details you can get on Settings Page of your Gmail Account

NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465

NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'Set your credentials of your Gmail Account

 NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "shank@gmail.com"

 NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "********"

 'Update the configuration fields
NewMail.Configuration.Fields.Update

'Set All Email Properties

With NewMail
  .Subject = "Test Mail"
  .From = "shank@gmail.com"
  For i = 1 To 2
      Text = Cells(i, 1).Value
      Text2 = Cells(i, 2).Value
      .To = Text
      .BCC = ""
      .TextBody = ""
      .AddAttachment Text2
      Text2 = Null
      .Send
  Next i

End With

End Sub
它从第一列中读取电子邮件地址,在第二列中,我共享了附件的地址。 当它向最后一个用户发送电子邮件时,它会从最上面一行附加所有附件。 e、 g:

对我来说也是如此sha@gwu它同时发送doc Test和Test2。

我只想为您的sha@gwu. 我的代码有什么问题???

将其更改为此

For i = 1 To 2

Set NewMail = New CDO.Message

'// Rest of code here...

With NewMail
  .Subject = "Test Mail"
  .From = "shank@gmail.com"

  Text = Cells(i, 1).Value
  Text2 = Cells(i, 2).Value
  .To = Text
  .BCC = ""
  .TextBody = ""
  .AddAttachment Text2
  Text2 = Null
  .Send

End With

Next
添加此行

With NewMail
  .Subject = "Test Mail"
  .From = "shank@gmail.com"
  For i = 1 To 2

  Text = Cells(i, 1).Value
  Text2 = Cells(i, 2).Value
  .To = Text
  .BCC = ""
  .TextBody = ""
  .Attachments.DeleteAll       ' <--------
  .AddAttachment Text2
  Text2 = Null
  .Send
  Next i

End With

End Sub
与NewMail
.Subject=“测试邮件”
.From=”shank@gmail.com"
对于i=1到2
文本=单元格(i,1)。值
Text2=单元格(i,2).值
.To=文本
.BCC=“”
.TextBody=“”

.Attachments.DeleteAll“您的
结尾和
下一个i
是错误的方式around@Macro...could你可以解释一下。。。我是VBA新手,这是我的第三个代码…我不确定要解释什么-交换
结束和
下一个我
。。。目前的情况是,代码甚至不应该编译,所以不确定您是如何运行的。您需要显示更多的代码,以便我们使用NewMail解释i=1到2。Subject=“Test Mail”。From=”shank@gmail.com“Text=单元格(i,1)。Value Text2=单元格(i,2)。Value.To=Text.BCC=”“.TextBody=“”.AddAttachment Text2.Send Text2=”“End With Next i Set NewMail=Nothing End sub如果有帮助,请单击左侧的勾号,不要忘记标记为答案
With NewMail
  .Subject = "Test Mail"
  .From = "shank@gmail.com"
  For i = 1 To 2

  Text = Cells(i, 1).Value
  Text2 = Cells(i, 2).Value
  .To = Text
  .BCC = ""
  .TextBody = ""
  .Attachments.DeleteAll       ' <--------
  .AddAttachment Text2
  Text2 = Null
  .Send
  Next i

End With

End Sub