群发带附件的VBA程序

这个VBA宏示例展示了如何创建一个程序,用于从Excel数据源中提取收件人信息,并通过Outlook自动发送带有附件的电子邮件。用户可以自定义邮件主题,宏会迭代表格中的每一行,将每条信息复制到新的邮件中并发送。
Sub emailmergewithattachments()
'
' emailmergewithattachments Macro
' 宏在 2007-9-28 由 longmb 创建
'
Dim Source As Document, Maillist As Document
Dim Datarange As Range
Dim Counter As Integer, i As Integer
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim mysubject As String, message As String, title As String
 
Set Source = ActiveDocument
Set Text = Source.Content
'Selection.WholeStory
 '   Selection.Copy
 
' Check if Outlook is running.  If it is not, start Outlook
On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
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 = "sub" 'InputBox(message, title)
 
' Iterate through the rows of the catalog mailmerge document, extracting the information
' to be included in each email.
Counter = 1
rowNum = Maillist.Tables(1).Rows.Count
While Counter <= rowNum
  
    Dim temDocument As Document
    Set temDocument = Documents.Add
   
   
    Maillist.Tables(1).Cell(Counter, 2).Range.Copy
    Selection.Paste
   
    Source.Sections.First.Range.Copy
    'Source.Activate
    'Source.Sections.First.Range.WholeStory
    'Selection.Copy
   
    'temDocument.Activate
    Selection.PasteAndFormat (wdPasteDefault)
   
   
    'Selection.Paste
    'Selection.MoveStart
   
    Set oItem = oOutlookApp.CreateItem(olMailItem)
    With oItem
        .Subject = mysubject
       
       
        .Body = ActiveDocument.Content
       
        Set Address = Maillist.Tables(1).Cell(Counter, 1).Range
        Address.End = addressDatarange.End - 1
        .To = Address
       
        Set emailName = Maillist.Tables(1).Cell(Counter, 2).Range
      
        '.Body = emailName
        'Selection.PasteAndFormat (wdPasteDefault)
       
       
   
       
        attachmentNum = Maillist.Tables(1).Columns.Count
        For i = 3 To attachmentNum
            Set Datarange = Maillist.Tables(1).Cell(Counter, i).Range
            Datarange.End = Datarange.End - 1
            .Attachments.Add Trim(Datarange.Text), olByValue, 1
        Next i
        .Send
    End With
    Set oItem = Nothing
    temDocument.Close wdDoNotSaveChanges
    Counter = Counter + 1
Wend
 
'  Close Outlook if it was started by this macro.
If bStarted Then
    oOutlookApp.Quit
End If
 
'Clean up
Set oOutlookApp = Nothing
'Source.Close wdDoNotSaveChanges
Maillist.Close wdDoNotSaveChanges
End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值