vba - 如何在 Excel/VBA 中通过电子邮件将工作表发送给自己(根据打开的 Outlook 帐户动态更改电子邮件)

标签 vba excel outlook

现在我有一个包含一些 VBA 的工作簿,当按下按钮时,它会向一个用户发送电子邮件(电子邮件地址是硬编码的)。效果很好。但是,我想知道是否可以将电子邮件“抄送”给按下电子邮件按钮的用户。它可能来自 10-15 个不同的人。

现在,下面的代码将通过电子邮件向“[email protected]”发送一份名为“Print”的工作表副本,并且在收件箱中它来自正确的用户。它能够以某种方式利用用户的电子邮件并自动为他们发送电子邮件,因此我认为他们也必须有一种方法来抄送自己。

所有电子邮件帐户都将位于 Microsoft Oulook 上。

这是向一个人发送电子邮件的代码(我从 http://www.rondebruin.nl/win/s1/outlook/amail2.htm 获取它):

  'Sub that emails the 3rd sheet in the body of an email
    Sub Mail_Sheet_Outlook_Body()
    Call UnProtect
    Application.ReferenceStyle = xlA1

    'RangetoHTML function is copied in the module after this sub.

        Dim rng As Range
        Dim OutApp As Object
        Dim OutMail As Object

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

        Set rng = Nothing

        Set rng = Sheets("Print").UsedRange

        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)

        On Error Resume Next

            With OutMail
                .To = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="640b160001161724091d0109050d084a070b09" rel="noreferrer noopener nofollow">[email protected]</a>" 
                .CC = ""                              
                .BCC = ""
                .Subject = "New Order from Employee"
                .HTMLBody = RangetoHTML(rng)   
                .Send   'or use .Display
             End With 
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
    Call Protect
    End Sub

因此,重申一下我要问的问题,无论如何,当 [email protected]发送订单/发送电子邮件,将其“抄送”给自己,并且何时 [email protected]做同样的事情,只是抄送他自己。根据打开工作簿的帐户动态更改。

最佳答案

尝试使用 application.Session.CurrentUser.Address 获取电子邮件 ID

Sub EmailWithCCTome()
Dim outlookobj As Object
Dim emailitem As Object
Set outlookobj = CreateObject("Outlook.Application")
Set emailitem = outlookobj.CreateItem(olMailItem)
With emailitem
.To = toemail
.CC = outlookobj.Session.CurrentUser.Address
End With

关于vba - 如何在 Excel/VBA 中通过电子邮件将工作表发送给自己(根据打开的 Outlook 帐户动态更改电子邮件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24757940/

相关文章:

excel - 在 Excel 中使用 And 进行条件格式设置?

C# VSTO Outlook 加载项 : What is a possible impact of not releasing the MailItem object

vba - 如何将超链接字符串从一个工作簿传递到另一个工作簿

excel - 强制 Excel 2007 将列中的所有值视为文本

excel - 优化 VBA 代码并提高性能

excel - 按表生成MDB中所有字段的列表

vba - 我如何知道规则何时完成处理?

debugging - 如何调试 Outlook Web 加载项?

excel - 使用 Excel VBA 获取类属性值

vba - 我的代码关闭了工作簿而不是窗口