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

use*_*447 2 excel outlook vba

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

现在,下面的代码将通过电子邮件发送给“orders@myemail.com”一份名为“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 = "orders@myemail.com" 
                .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
Run Code Online (Sandbox Code Playgroud)

所以重申我的问题,无论如何,当 user1@email.com 发送订单/发送电子邮件时,它“抄送”给自己,而当 user2@email.com 做同样的事情时只有cc自己。根据打开工作簿的帐户动态更改。

Din*_*nna 5

尝试使用 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
Run Code Online (Sandbox Code Playgroud)