以前在网上看到的,不知有用否??? Sub eMailMergeWithAttachments() Dim docSource As Document, docMaillist As Document,docTempDoc As Document Dim rngDatarange As Range Dim i As Long, j As Long Dim lSectionsCount As Long Dim bStarted As Boolean Dim oOutlookApp As Outlook.Application Dim oItem As Outlook.MailItem Dim oAccount As Outlook.Account Dim sMySubject As String, sMessage As String,sTitle As String '将当前文档设置为源文档(主文档) Set docSource = ActiveDocument '检查Outlook是不是打开了。如果未打开的话,就打开新的Outlook On Error Resume Next Set oOutlookApp = GetObject(,"Outlook.Application") If Err <> 0 Then Set oOutlookApp =CreateObject("Outlook.Application") bStarted = True End If '打开保存有客人的邮件地址和需要发送的附件的路径的word文档。 With Dialogs(wdDialogFileOpen) .Show End With '将该文档设置为客户邮件(附件)列表文档 Set docMaillist = ActiveDocument '设置发送邮件的账户(账户必须已经在Outlook中设置好了) '注意:如果你的Outlook版本低于2007,使用设置发送邮件的账户可能会导致错误, '建议将下面的Set oAccount =oOutlookApp.Session.Accounts.Item("someone@examplemail.com")语句删除 Set oAccount =oOutlookApp.Session.Accounts.Item("someone@examplemail.com") '显示一个输入框,询问并让用户输入邮件主题 sMessage = "请为要发送的邮件输入邮件主题。" sTitle = "输入邮件主题" sMySubject = InputBox(sMessage, sTitle) '循环查找源文档中所有的节(每一节为一封邮件内容),以及循环查找邮件列表文档中所有的客户信息, '以便用于插入到生成的邮件中 lSectionsCount = docSource.Sections.Count - 1 '当源文档中的节数仅有1时,lSectionsCount=0,将导致程序无法正常运行。 '为了保证当源文档只有1节时程序能正常运行,必须使lSectionsCount至少等于1 If lSectionsCount = 0 Then lSectionsCount = 1 For j = 1 To lSectionsCount Set oItem =oOutlookApp.CreateItem(olMailItem) With oItem '注意:如果你的Outlook版本低于2007,使用设置发送邮件的账户可能会导致错误, '建议将下面的.SendUsingAccount = oAccount语句删除 .SendUsingAccount= oAccount .Subject= sMySubject '把邮件正文复制到剪贴板 docSource.Sections(j).Range.Copy '显示邮件 .Display '注意:使用以下方法,必须保证outlook正在使用的是Word编辑器 SetdocTempDoc = oOutlookApp.ActiveInspector.WordEditor '粘贴邮件正文到outlook docTempDoc.Range.Paste SetrngDatarange = docMaillist.Tables(1).Cell(j, 1).Range rngDatarange.End= rngDatarange.End - 1 .To= rngDatarange Fori = 2 To docMaillist.Tables(1).Columns.Count SetrngDatarange = docMaillist.Tables(1).Cell(j, i).Range rngDatarange.End= rngDatarange.End - 1 .Attachments.AddTrim(rngDatarange.Text), olByValue, 1 Nexti '如果需要立即发送邮件,请把下面一行的注释去掉 '.Send End With Set oItem = Nothing Next j docMaillist.Close wdDoNotSaveChanges '如果Outlook是由该宏打开的,则关闭Outlook If bStarted Then oOutlookApp.Quit End If MsgBox "共发送了 " & lSectionsCount & " 封邮件。" '清空Outlook实例 Set oOutlookApp = Nothing End Sub 另外一个方法是把word的正文保存为htm格式,然后通过FileSystemObject读取该文件内容。坏处是,所有邮件的正文的内容都是一样的。这里也把代码说一下。 Function GetBoiler(ByVal sFile As String) As String Dim fso As Object Dim ts As Object Set fso =CreateObject("Scripting.FileSystemObject") Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2) GetBoiler = ts.Readall ts.Close End Function 记得要把eMailMergeWithAttachments过程中的 .Body = docSource.Sections(j).Range.Text 改成 .HtmlBody = GetBoiler("D:\myattachment.pdf")
|