我需要大量电子邮件,其中包含每个收件人的URL链接,以获取他们必须填写的调查问卷。
这或多或少是代码。我收到
运行时错误"'91'对象变量或未设置块"。
关于 Omail 未设置但已设置的内容。
<小时 />Sub test()
Dim OApp As Object, OMail As Object, signature As String
Set OApp = CreateObject("Outlook.Application")
For i = 2 To Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
Set OMail = OApp.CreateItem(0)
With OMail
.Display
End With
signature = OMail.Body
With OMail
.To = Sheet1.Cells(i, 1).Value
.Subject = Sheet1.Cells(i, 2).Value
'.Attachments.Add
.Body = "Hello world" & Sheet1.Cells(i, 3).Value & vbNewLine & signature
.Send
End With
Set OMail = Nothing
Set OApp = Nothing
Next
End Sub
我想发送多封带有可自定义正文的电子邮件,但我不想丢失签名。
您正在将OApp
设置为在循环中Nothing
。
整理了一下:
Sub test()
Dim OApp As Object, OMail As Object, signature As String
Set OApp = CreateObject("Outlook.Application")
For i = 2 To Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
Set OMail = OApp.CreateItem(0)
With OMail
.Display
signature = .Body
.To = Sheet1.Cells(i, 1).Value
.Subject = Sheet1.Cells(i, 2).Value
'.Attachments.Add
.Body = "Hello world" & Sheet1.Cells(i, 3).Value & vbNewLine & signature
.Send
End With
Set OMail = Nothing
'Set OApp = Nothing '<< don't do this!
Next
End Sub