对象'_Worksheet'的方法'MailEnvelope'失败

时间:2015-03-20 16:22:58

标签: excel-vba ms-word outlook vba excel

我遇到以下代码问题。我尽可能多地评论它。在此先感谢您的帮助。

它挂在:

Set MailSendItem = doc.MailEnvelope.Item

它给出错误的方法' MailEnvelope'对象' _Worksheet'失败。我尝试在发送之前保存工作簿,但它没有解决问题。这个问题在我加入时才开始:

.Attachments.Add ("H:\Thought Pieces\Small Cap Liquidity\A Closer Look at Small Cap Liquidity.pdf")

挂起后,我可以点击简历,它将继续迭代Excel表格并发送电子邮件。代码工作完善否则。 On Error Resume Next不继续发送,只手动恢复。

Sub SendOutlookMessages()

 'Dimension variables.
 Dim OL As Object, MailSendItem As Object
 Dim W As Object
 Dim MsgTxt As String, SendFile As String
 Dim ToRangeCounter As Variant

 Set wd = CreateObject("Word.Application")
 Dim doc As Word.Document

 'On Error Resume Next

 'Assigns Word file to send
 Set wd = GetObject(, "Word.Application")
 If wd Is Nothing Then
 Set wd = CreateObject("Word.Application")
 blnWeOpenedWord = True
 End If
 Set doc = wd.Documents.Open _
 (Filename:="H:\Thought Pieces\Small Cap Liquidity\A Closer Look at Small Cap Liquidity.doc", ReadOnly:=False)
 Set itm = doc.MailEnvelope.Item

 'Starts Outlook session
 Set OL = CreateObject("Outlook.Application")
 Set MailSendItem = doc.MailEnvelope.Item


 'Creates message
 For Each xRecipient In Range("tolist")
    With MailSendItem
     .Subject = Sheets("Sheet1").Range("subjectcell").Text
     .Body = MsgTxt
     .To = xRecipient
     .Cc = xRecipient.Offset(0, 6)
     .Attachments.Add ("H:\Thought Pieces\Small Cap Liquidity\A Closer Look at Small Cap Liquidity.pdf")
     .Send
    End With
    Set MailSendItem = doc.MailEnvelope.Item
    Application.Wait (Now + TimeValue("00:00:30"))
 Next xRecipient


 'Ends Outlook session
 Set OL = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

经过几个小时的阅读和测试......答案如下:

我必须关闭文档的先前实例(如果我不想继续打开新的MSWord进程,则使用当前打开的MSWord对象。)

我编辑了'创建消息部分:

'Creates message
 For Each xRecipient In Range("tolist")
    With MailSendItem
     .Subject = Sheets("Sheet1").Range("subjectcell").Text
     .Body = MsgTxt
     .To = xRecipient
     .Cc = xRecipient.Offset(0, 6)
     .Attachments.Add ("H:\Thought Pieces\Small Cap Liquidity\A Closer Look at Small Cap Liquidity.pdf")
     .Send
    End With
    doc.Close SaveChanges:=wdDoNotSaveChanges

    Set wd = GetObject(, "Word.Application")

    Set doc = wd.Documents.Open _
 (Filename:="H:\Thought Pieces\Small Cap Liquidity\A Closer Look at Small Cap Liquidity.doc", ReadOnly:=False)
    Set MailSendItem = doc.MailEnvelope.Item
    Application.Wait (Now + TimeValue("00:00:30"))
 Next xRecipient

需要注意的部分是:

    Set wd = GetObject(, "Word.Application")

    Set doc = wd.Documents.Open _
 (Filename:="H:\Thought Pieces\Small Cap Liquidity\A Closer Look at Small Cap Liquidity.doc", ReadOnly:=False)
    Set MailSendItem = doc.MailEnvelope.Item