对象“_Worksheet”的方法 'MailEnvelope' 失败
Method 'MailEnvelope' of object '_Worksheet' failed
我在使用以下代码时遇到问题。我尽可能多地评论它。预先感谢您的帮助。
它挂在:
Set MailSendItem = doc.MailEnvelope.Item
它给出错误方法 'MailEnvelope' 对象 '_Worksheet' 失败。我尝试在发送前保存工作簿,但没有解决问题。这个问题只有在我合并后才开始:
.Attachments.Add ("H:\Thought Pieces\Small Cap Liquidity\A Closer Look at Small Cap Liquidity.pdf")
挂起后,我只需点击恢复,它就会继续遍历 Excel sheet 并发送电子邮件。否则代码工作完美。 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
我在使用以下代码时遇到问题。我尽可能多地评论它。预先感谢您的帮助。
它挂在:
Set MailSendItem = doc.MailEnvelope.Item
它给出错误方法 'MailEnvelope' 对象 '_Worksheet' 失败。我尝试在发送前保存工作簿,但没有解决问题。这个问题只有在我合并后才开始:
.Attachments.Add ("H:\Thought Pieces\Small Cap Liquidity\A Closer Look at Small Cap Liquidity.pdf")
挂起后,我只需点击恢复,它就会继续遍历 Excel sheet 并发送电子邮件。否则代码工作完美。 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