简体   繁体   English

Excel可以基于单元格值向不同的用户发送电子邮件吗?

[英]Can Excel send an email to different users based cell value?

I wanted to know if it's possible/viable/logical before attempting to do it. 我想知道在尝试之前是否可行/可行/合乎逻辑。

I have a shared document on a network drive that about 20-30 people work on/update. 我在网络驱动器上有一个共享文档,大约有20-30个人正在处理/更新。

There are multiple fields, multiple sheets. 有多个字段,多个工作表。

Across a number of sheets there is a USER colum and a STATUS column. 在许多工作表上都有一个USER列和一个STATUS列。

ROW   ITEM               USER              STATUS
----------------------------------------------------
1     Web Job 1          John             In Progress
2     Web Service A      Mike             Delivered
3     WPF Job 2          Amy              In Progress
4     Test Job 1         Brian            Delivered

When a status row is updated with 'Delivered' (again, any of the 30 people working on this workbook can change the status), is it possible for a VBA macro so fire off an email to (in this example) Mike and Brian saying 'Your work items have been delivered'? 当状态行更新为“已发送”(同样,使用此工作簿的30个人中的任何人都可以更改状态)时,是否可以使用VBA宏,因此向(在此示例中)迈克和布莱恩说一封电子邮件“您的工作项目已交付”?

My concern is that there are so many hands stirring the pot, so to speak, that it's not practical to automate the notification process based on the workbook being updated. 我担心的是,有太多人在搅拌锅,可以这么说,基于正在更新的工作簿来自动化通知过程是不切实际的。

Is it worth pursuing or should I forgo this altogether? 是值得追求的还是我应该完全放弃呢?

Yes, it's possible, but as mehow says, it may not be the best idea with a shared Workbook. 是的,这是可能的,但是如我所知,使用共享工作簿可能不是最好的主意。 If you do wish to go ahead with it, here's how I would do it. 如果您确实希望继续进行下去,这就是我的做法。 For the purposes of this solution, I am assuming that each of the users have Outlook installed on their machines. 出于此解决方案的目的,我假设每个用户的计算机上都安装了Outlook。

First, open the VBA IDE, click "Tools" ---> "References..." and check the box next to "Microsoft Outlook 14.0 Object Library" (you may have a different version number) to add a reference to Outlook COM. 首先,打开VBA IDE,单击“工具” --->“参考...”,然后选中“ Microsoft Outlook 14.0对象库”(您可能有不同的版本号)旁边的框,以添加对Outlook COM的引用。

Second, you can use some variation of the below code to generate an email. 其次,您可以使用以下代码的一些变体生成电子邮件。 I've used the HTMLBody property because I generally use html tags to format automatically generated emails, but you may just want to use plain text. 我之所以使用HTMLBody属性,是因为我通常使用html标签来格式化自动生成的电子邮件,但是您可能只想使用纯文本。 Create a module and add this code to it. 创建一个模块并将此代码添加到其中。

Public Sub sendMail(strTo As String, _
                strSubject As String, _
                strBodyText As String, _
                Optional strCC As String = "", _
                Optional oAttachments As Collection = Nothing)
'This function creates an email and immediately sends it.

    Dim Attachment As Variant
    Dim oMailItem As Outlook.MailItem

    'Create the email
    Set oMailItem = Outlook.Application.CreateItem(olMailItem)

    'Populate the email properties
    With oMailItem
        .Subject = strSubject
        .To = strTo
        'Add the CC recipients, if any
        .CC = strCC
        .HTMLBody = strBodyText
        .BodyFormat = olFormatHTML


        'Add the attachments, if any
        If Not (oAttachments Is Nothing) Then
            For Each Attachment In oAttachments
                .Attachments.Add (Attachment)
            Next Attachment
        End If

        'Send it!
        .Send
    End With

    'Release the object
    Set oMailItem = Nothing
End Sub

Third, you will need to add a Worksheet_Change event handler to each worksheet that contains a status column that should trigger an email. 第三,您需要向每个工作表添加一个Worksheet_Change事件处理程序,该事件处理程序包含应触发电子邮件的状态列。 I recommend using the pull-downs above the VBA code window to get the correct function declaration by selecting "Worksheet" in the left pull-down and "Change" in the right one. 我建议使用VBA代码窗口上方的下拉菜单,通过在左侧的下拉菜单中选择“工作表”,在右侧的下拉菜单中选择“更改”,以获取正确的函数声明。 In the function, you need to make sure that the Target is in the STATUS column and that it matches the string value you are looking for. 在函数中,您需要确保“ Target位于“状态”列中,并且与您要查找的字符串值匹配。 I leave it as an excercise to you to put it all together, but let me know if you have any questions. 我将它作为锻炼的技巧保留给您,但是如果您有任何疑问,请告诉我。

There are some gotchas to be aware of: 有一些陷阱要注意:

  • The change event fires immediately after the user leaves the target cell after making the change, meaning so will the email. 用户进行更改后离开目标单元格后立即触发更改事件,这意味着电子邮件也会触发。 This means that even if someone accidentally changes the status column, the email will still send. 这意味着,即使有人不小心更改了状态栏,电子邮件仍会发送。

  • The code is running on each individual users' machine, so if two users change the same STATUS cell, the email will fire from both machines (not quite sure how the Excel multi-user change conflict resolution affects this). 该代码在每个用户的计算机上运行,​​因此,如果两个用户更改同一状态单元,则电子邮件将从两台计算机上触发(不太确定Excel多用户更改冲突解决方案对此有何影响)。

  • I believe that if Outlook is not running, it will be started up for the email to be sent. 我相信,如果Outlook未运行,它将启动以发送电子邮件。 Excel may appear to hang while that's happening. 在这种情况下,Excel可能似乎挂起了。

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

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