简体   繁体   English

Excel加载项/宏以发送大量电子邮件

[英]Excel Add-In/Macro to send mass email

So I have a task to automate. 因此,我有一项任务要自动化。 We have a protected spreadsheet (users only have 'read' access to) that get's updated by admins from time to time in order to add/remove users from a paid subscription mailing list. 我们有一个受保护的电子表格(用户仅具有“读取”访问权限),管理员不时对其进行更新,以便从付费订阅邮件列表中添加/删除用户。 I'm trying to make the process of sending these emails out simpler to speed up the process and eliminate the potential of human error getting involved. 我正在尝试简化发送这些电子邮件的流程,以加快流程并消除人为错误的可能性。

So email addresses are listed under the 'C' column, lists can be as long as in the tens of thousands, or it may only be 1 or 2. The workbook has several sheets that specify the data set that the subscribers subscribe to. 因此,电子邮件地址列在“ C”列下,列表可以长达几万个,也可以只有1个或2个。该工作簿有多个工作表,用于指定订阅者订阅的数据集。 So I put something together that worked 所以我把一些有用的东西放在一起

'This function will grab the information the macro asks for
Function RangeToString(ByVal myRange As Range) As String
RangeToString = ""
If Not myRange Is Nothing Then
    Dim myCell As Range
    For Each myCell In myRange
        RangeToString = RangeToString & "; " & myCell.Value
    Next myCell
    'Remove extra comma
    RangeToString = Right(RangeToString, Len(RangeToString) - 1)
End If
End Function

Sub EmailTest1()
Dim OutApp As Object
Dim OutMail As Object
Dim strSubject As String
Dim myString As String
Dim rng As Range
Dim strCopy As String

'Sheet1 would be Sheet2/3/4/etc. depending on what list we're pulling from.
Set rng = Sheet1.Range("c2:c90000")
myString = RangeToString(rng)
strCopy = "internal.private@email.com; internal1.private@email.co; 
internal2.private@email.co"
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItemFromTemplate("\\Domain\EmailTemplate\oft\test1.oft")

On Error Resume Next
With OutMail
    .BCC = myString + strCopy
    .Display
    '.Send
End With

On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Then I had repeats of the second part that specify the different lists/email templates as individual macros within the Add-In. 然后,我重复了第二部分,将不同的列表/电子邮件模板指定为外接程序中的各个宏。 (.Send will not be commented out when I do finally get the results I want). (当我最终获得想要的结果时,.send将不会被注释掉)。

So this works perfectly, when the macro specifies and embeds a workbook into itself. 因此,当宏指定工作簿并将其嵌入到自身中时,此方法非常有效。 So for a while, I thought that it was pulling everything fine, until I used a blank workbook, and it still pulled the data I wanted, so I checked and double checked that there was no reference to the original workbook, and then I discovered that the workbook was built into the macro. 所以有一段时间,我以为一切正常,直到使用空白的工作簿,它仍然提取我想要的数据,所以我检查并再次检查是否没有对原始工作簿的引用,然后我发现该工作簿已内置到宏中。 I tried rebuilding the add-in using the same code, and it just doesn't work. 我尝试使用相同的代码重建外接程序,但它不起作用。

So my question is, is there a way to build this macro so that it'll work on any active workbook? 所以我的问题是,有没有一种方法可以构建此宏,使其可以在任何活动的工作簿上使用? I imagine there has got to be a simple thing to click on or something else I'm overlooking. 我想必须要单击一个简单的东西,或者我要忽略的其他东西。 I'm working with Excel 2016. 我正在使用Excel 2016。

first of all, why didn't you just make a macro-embedded template where you have a form that connects any active workbook. 首先,为什么不只是制作一个宏嵌入式模板,在该模板中您具有连接任何活动工作簿的表单。

dim ws as workbook 昏暗的ws作为工作簿

set ws=activeworkbook 设置ws = activeworkbook

so basically make a form that is modular then on a label click event put that code. 因此,基本上制作一个模块化的表单,然后在标签单击事件上放上该代码。

then an execute button so that you can determine if you connect the right workbook before you start the email sending automation 然后单击执行按钮,以便您可以在启动自动发送电子邮件之前确定是否连接了正确的工作簿

I think you can adapt this to suit your needs. 我认为您可以根据自己的需要进行调整。

Make a list in Sheets("Sheet1") with : 使用以下方法在Sheets(“ Sheet1”)中列出:

In column A : Names of the people
In column B : E-mail addresses
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)

The Macro will loop through each row in "Sheet1" and if there is a E-mail address in column B and file name(s) in column C:Z it will create a mail with this information and send it. 宏将循环遍历“ Sheet1”中的每一行,如果B列中有一个电子邮件地址,而C:Z列中有一个文件名,则它将创建带有此信息的邮件并发送。

Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .to = cell.Value
                .Subject = "Testfile"
                .Body = "Hi " & cell.Offset(0, -1).Value

                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                    If Trim(FileCell) <> "" Then
                        If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
                        End If
                    End If
                Next FileCell

                .Send  'Or use .Display
            End With

            Set OutMail = Nothing
        End If
    Next cell

    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

https://www.rondebruin.nl/win/s1/outlook/amail6.htm https://www.rondebruin.nl/win/s1/outlook/amail6.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM