【问题标题】:Method 'MailEnvelope' of object '_Worksheet' failed对象“_Worksheet”的方法“MailEnvelope”失败
【发布时间】:2015-03-20 16:22:57
【问题描述】:

下面的代码有问题。我尽可能多地评论它。提前感谢您的帮助。

它挂在:

Set MailSendItem = doc.MailEnvelope.Item

它给出了错误方法“_Worksheet”的方法“MailEnvelope”失败。我尝试在发送之前保存工作簿,但没有解决问题。这个问题只有在我合并后才开始:

.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

【问题讨论】:

    标签: excel ms-word outlook vba


    【解决方案1】:

    经过数小时的阅读和测试……答案如下:

    我必须关闭文档的前一个实例(如果我不想继续打开新的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多