【发布时间】:2016-11-03 07:58:16
【问题描述】:
我已经使用这个宏好几天了,当我认为它运行良好时,我发现它仅适用于前 200 封电子邮件。之后,它会创建具有正确收件人和主题的电子邮件,但没有文本和附件。在测试了不同的场景之后,似乎(某种)Outlook 内存被填满了,但我不知道是什么以及如何清除它(我添加了 oItem 和 oOutlookApp = 没有成功)。我可以让它工作的唯一方法是如果我关闭 Outlook 并使用 200 和以下电子邮件再次运行宏。 有任何想法吗? 谢谢
编辑: 1- 我还尝试使用接受的答案overhere 在循环结束时清除剪贴板,唉,没有结果。
2- 我发现这个answer 似乎与我的问题有关。但是有两个主要区别:我的宏从 Word 运行到 Outlook(而不是 Outlook 到 Excel)并且我没有收到错误消息;超过 #200 的电子邮件只会被创建为空。所以我不知道这里是否/如何提供帮助。
3- 按照 niton 的说明,现在有一条错误消息。进步我猜...
突出显示的行是
.Attachments.Add Trim(Datarange.Text), olByValue, 1
它会在第 200 封电子邮件中这样做。
' MailMerge Macro
'
'
Sub MergeWithAttachments()
Dim Source As Document, Maillist As Document, TempDoc As Document
Dim Datarange As Range
Dim i As Long, j As Long
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim mysubject As String, message As String, title As String
Dim mailWord As Object
Dim oData As New DataObject
Set Source = ActiveDocument
' Check if Outlook is running. If it is not, start Outlook
On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
' Open the catalog mailmerge document
With Dialogs(wdDialogFileOpen)
.Show
End With
Set Maillist = ActiveDocument
' Show an input box asking the user for the subject to be inserted into the email messages
message = "Enter the subject to be used for each email message." ' Set prompt.
title = "Email Subject Input" ' Set title.
' Display message, title
mysubject = InputBox(message, title)
' Iterate through the Sections of the Source document and the rows of the catalog mailmerge document,
' extracting the information to be included in each email.
If MsgBox("Are you adding cc email recipients?", vbYesNo, "CC email") = vbYes Then
If MsgBox("Are your cc email recipients in the second column from the left?", vbYesNo, "CC in second column") = vbYes Then
GoTo Add_cc
Else:
If MsgBox("Cc email recipients need to be in the second column. Please rework your directory accordingly.", vbOKOnly, "Cancelling Mail Merge") = vbOK Then
Exit Sub
End If
No_cc:
For j = 1 To Source.Sections.Count - 1
Source.Sections(j).Range.Copy
Set oItem = oOutlookApp.CreateItem(olMailItem)
Set mailWord = oItem.GetInspector.WordEditor
With oItem
.Subject = mysubject
mailWord.Range.PasteAndFormat (wdFormatOriginalFormatting)
Set Datarange = Maillist.Tables(1).Cell(j, 1).Range
Datarange.End = Datarange.End - 1
.To = Datarange
For i = 2 To Maillist.Tables(1).Columns.Count
Set Datarange = Maillist.Tables(1).Cell(j, i).Range
Datarange.End = Datarange.End - 1
.Attachments.Add Trim(Datarange.Text), olByValue, 1
Next i
.Send
End With
Set oItem = Nothing
Next j
GoTo Merge_finished
Add_cc:
For j = 1 To Source.Sections.Count - 1
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
.Subject = mysubject
Set Datarange = Maillist.Tables(1).Cell(j, 1).Range
Datarange.End = Datarange.End - 1
.To = Datarange
'code for adding cc emails. Currenlty set to read column 2 as cc emails
Set Datarange = Maillist.Tables(1).Cell(j, 2).Range
Datarange.End = Datarange.End - 1
.CC = Datarange.Text
Source.Sections(j).Range.Copy
Set mailWord = oItem.GetInspector.WordEditor
mailWord.Range.PasteAndFormat (wdFormatOriginalFormatting)
For i = 2 To Maillist.Tables(1).Columns.Count
Set Datarange = Maillist.Tables(1).Cell(j, i).Range
Datarange.End = Datarange.End - 1
.Attachments.Add Trim(Datarange.Text), olByValue, 1
Next i
.Send
End With
Set oItem = Nothing
Next j
Merge_finished:
End If
Else: GoTo No_cc
End If
Maillist.Close wdDoNotSaveChanges
' Close Outlook if it was started by this macro.
If bStarted Then
oOutlookApp.Quit
End If
MsgBox Source.Sections.Count - 1 & " messages have been sent."
'Clean up
Set oOutlookApp = Nothing
End Sub
【问题讨论】:
-
可以分享一下word文档吗?为什么你的第二个循环中有
Set oOutlookApp = CreateObject("Outlook.Application")?我认为你不需要它,删除它。我还看到您在剪贴板中添加了大量数据,请确保将其清除以避免错误 -
在 Set oOutlookApp = GetObject(, "Outlook.Application") 之后设置一个 On Error Goto 0。只要不再需要 On Error Resume Next,就应该总是有 On Error Goto 0。
-
@Om3r - 无法共享 word 文档,因为它包含电子邮件地址。此外,我已将问题范围缩小到 Outlook,而不是 Word 或剪贴板。当我累了并且拼命尝试任何东西时,我添加了 oOoutloopApp 的东西!将删除它!
-
@niton - 我会这样做,但我怀疑它会解决我遇到的问题,因为这篇文章只是打开 Outlook,如果它还没有打开(我认为)。
-
我不是指真实数据,只是添加假数据
标签: vba ms-word outlook-2010