簡體   English   中英

對象“ _Worksheet”的方法“ MailEnvelope”失敗

[英]Method 'MailEnvelope' of object '_Worksheet' failed

我的下面的代碼有問題。 我盡我所能評論了。 在此先感謝您的幫助。

它掛在:

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

經過數小時的閱讀和測試之后,答案如下:

我必須關閉文檔的先前實例(如果我不想繼續打開新的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM