服务器执行失败创建 Outlook 应用程序

时间:2021-01-09 12:24:15

标签: excel vba outlook

我正在尝试使用 Outlook15 从邮件列表发送。

我写了以下内容,循环遍历 16 个带有邮件地址的单元格,并附上 pdf 文件。

我明白了:

<块引用>

"运行时错误'-2146959355(80080005)':
服务器执行失败"

当我调试时 Set outlookapp = CreateObject("Outlook.Application") 被高亮显示。

Sub SndEmail()
'define variables and their types'
Dim address As String
Dim subject As String
Dim message As String
Dim filename As String
Dim outlookapp As Object
Dim outlookmailitem As Object
Dim myAttachments As Object
Dim path As String
Dim lastrow As String
Dim attachment As String
'x will be row index we loop through'
Dim x As Integer

x = 61

Do While x < 77
        
    Set outlookapp = CreateObject("Outlook.Application")
    Set outlookmailitem = outlookapp.CreateItem(0)
    Set myAttachments = outlookmailitem.Attachments
    'taking the file path from cells in excel'
    path = sheet1.Cells(x, 4)
    address = sheet1.Cells(x, 3)
    subject = "hello world"
    attachment = path
        
    outlookmailitem.To = address
    outlookmailitem.CC = ""
    outlookmailitem.BCC = ""
    outlookmailitem.subject = subject
    'taking the body content from cell 56'
    outlookmailitem.Body = sheet1.Cells(56, 3)
                
    myAttachments.Add (attachment)
    outlookmailitem.Display
    outlookmailitem.Send
        
    address = ""
    x = x + 1
Loop
        
Set outlookapp = Nothing
Set outlookmailitem = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

您正在为每个循环创建这些对象的新实例:

Set outlookapp = CreateObject("Outlook.Application")
Set outlookmailitem = outlookapp.CreateItem(0)
Set myAttachments = outlookmailitem.Attachments

如果您熟悉 C/C++、Java 等,VBA 中的 Set 等于 C/C++/Java 中的 new

因此将这些行放在循环之外(之前)

相关问题