【发布时间】: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
【问题讨论】: