vba 如何抄送发件人?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28476009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 06:11:29  来源:igfitidea点击:

How to CC the sender?

vbaemailoutlook-vba

提问by Billo

I have code to send email. I'm struggling to CC the sender? If I mail I should get the CC, if my colleague mails he should get the CC.

我有发送电子邮件的代码。我正在努力抄送发件人?如果我发邮件,我应该得到 CC,如果我的同事发邮件,他应该得到 CC。

Our usernames aren't firstname.lastname but our email addresses are.

我们的用户名不是 firstname.lastname,而是我们的电子邮件地址。

Sub SendPDF()

Dim strPath As String, strFName As String
Dim OutApp As Object, OutMail As Object

Title = Format(Now(), "dd/mm/yyyy") & " - " & ActiveSheet.Name & ""

strPath = Environ$("temp") & "\" 'Or any other path, but include trailing "\"

strFName = ActiveWorkbook.Name
strFName = Format(Now(), "yyyymmdd") & " - " & ActiveSheet.Name & ".pdf"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    strPath & strFName, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

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

On Error Resume Next
With OutMail
    .To = "[email protected]"
    .CC = ""
    .BCC = ""
    .Subject = Title
    .body = "Please see attached"
    .Attachments.Add strPath & strFName
    '.Display
    .Send
End With

Kill strPath & strFName
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

回答by Eugene Astafiev

You can use the CurrentUserproperty of the Namespace class to get the currently logged-on user as a Recipient object. Then you can get the Addressproperty value which representing the e-mail address of the Recipient.

您可以使用Namespace 类的CurrentUser属性将当前登录的用户作为 Recipient 对象获取。然后您可以获得代表收件人电子邮件地址的地址属性值。

.CC = nameSpace.CurrentUser.Address;

Also you may find the How To: Fill TO,CC and BCC fields in Outlook programmaticallyarticle helpful.

此外,您可能会发现如何:以编程方式填写 Outlook 中的收件人、抄送和密件抄送字段一文很有帮助。

回答by user11967079

Sub email()
    Dim a As Integer
    Dim objOutlook As Object
    Dim objMail As Object
    Dim rngTo As Range
    Dim rngCc As Range
    Dim rngBcc As Range
    Dim rngSubject As Range
    Dim rngAttach As Range
    Dim rngBody As Range

    Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(0)

    With ActiveSheet
        Set rngTo = .Range("B1")
        Set rngCc = .Range("B2")
        Set rngBcc = .Range("B3")
        Set rngSubject = .Range("B4")
        Set rngAttach = .Range("B5")
        Set rngBody = .Range("B6")

    End With

    With objMail
        .To = rngTo.Value
        .Cc = rngCc.Value
        .Bcc = rngBcc.Value
        .Subject = rngSubject.Value
        .Attachments.Add rngAttach.Value
        .Body = rngBody.Value
        .Display
    End With

    Set objOutlook = Nothing
    Set objMail = Nothing
    Set rngTo = Nothing
    Set rngCc = Nothing
    Set rngBcc = Nothing
    Set rngSubject = Nothing
    Set rngAttach = Nothing
    Set rngBody = Nothing
End Sub