首页 文章

Office 2013 - VBA电子邮件不显示To / CC / BCC变量

提问于
浏览
0

我正在通过VBA Outlook.Application Reference从Excel创建电子邮件 . 每封电子邮件都填充了我的Excel工作表中的数据,然后放入To / CC / BCC / Subject / Body字段 .

现在,在Office 2010中运行此代码时,它可以顺利运行,但在Office 2013中包含To / CC / BCC /等的变量 . 显示时,数据不会显示在实际的电子邮件中 .

Office 2013中此参考更改了吗?

Sub MailSheet()

    Dim OutApp As Object
    Dim outMail As Object
    Dim rng As Range

    ' set required variables
    Set Sourcewb = ActiveWorkbook
    Set Property = ActiveWorkbook.Sheets("Settings").Range("B4")
    Set Holidex = ActiveWorkbook.Sheets("Settings").Range("B5")
    Set SendTo = ActiveWorkbook.Sheets("Settings").Range("B29")
    Set SendCC = ActiveWorkbook.Sheets("Settings").Range("B30")
    Set rng = Sheets("Mail").Range("A1:F80")

    ' set email variables
    Set OutApp = CreateObject("Outlook.Application")
    Set outMail = OutApp.CreateItem(0)

' some code

        ' get ready to mail
        With outMail
            .To = SendTo
            .ReplyRecipients.Add ""
            .CC = SendCC
            .BCC = ""
            .Subject = Holidex & " - Daily Email"
            .HTMLBody = RangetoHTML(rng)

            ' display email before sending
            .Display   '.Send or use .Display
        End With

' some code

    ' Clean up
    Set outMail = Nothing
    Set OutApp = Nothing

end Sub

2 回答

  • 0

    而不是创建Outlook对象,尝试引用Outlook库(工具 - >引用,然后选择 Microsoft Outlook xx.x Object Library ) . 然后,您可以参考如下:

    Sub SendAnEmail()
    
        Dim oOlApp As Outlook.Application: Set oOlApp = Outlook.Application
        Dim oMailItem As Outlook.MailItem: Set oMailItem = oOlApp.CreateItem(olMailItem)
    
        oMailItem.To = "myemail@test.com"
        oMailItem.CC = ""
        oMailItem.BCC = "myemail@test.com"
        oMailItem.Subject = Sheet1.Cells(15, "D")
        oMailItem.HTMLBody = "Again .. testing"
        oMailItem.Display
    
        Set oMailItem = Nothing
        Set oOlApp = Nothing
    
    End Sub
    

    您可以在sub中添加此代码,也可以使用参数从Sub调用此Sub

  • 0

    不确定我可以直接帮助你,但我确实有一些我在网上找到的代码,我知道这个代码适用于Outlook 2016,会在这里分享它以防万一:

    Sub OutlookMail_1()
    'Automate Sending Emails from Excel, using Outlook.
    'Send text and also contents from the host workbook's worksheet range
    ' as Mail Body, and add an attachment with the mail.
    'Automating using Early Binding: Add a reference to the Outlook Object Library
    ' in Excel (your host application) by clicking Tools-References in VBE,
    ' which will enable using Outlook's predefined constants.
    'Once this reference is added, a new instance of
    ' Outlook application can be created by using the New keyword.
    
    'variables declared as a specific object type
    '  ie. specific to the application which is being automated:
    Dim applOL As Outlook.Application
    Dim miOL As Outlook.MailItem
    Dim recptOL As Outlook.Recipient
    Dim ws As Worksheet
    Dim name As String
    Dim email As String
    Dim nominees As Range
    Dim number As String
    
    'set worksheet:
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    'Create a new instance of the Outlook application.
    ' Set the Application object as follows:
    Set applOL = New Outlook.Application
    
    'create mail item:
    Set miOL = applOL.CreateItem(olMailItem)
    
    'Add mail recipients, either the email id or their name in your address book.
    ' Invalid ids will result in code error.
    Set recptOL = miOL.Recipients.Add("Main recipient email")
    recptOL.Type = olTo
    
    Set recptOL = miOL.Recipients.Add("BCC Email")
    recptOL.Type = olbcc
    
    Set recptOL = miOL.Recipients.Add("BCC Email")
    recptOL.Type = olbcc
    
    Set recptOL = miOL.Recipients.Add("BCC Email")
    recptOL.Type = olbcc
    
    'with the mail item:
    With miOL
    
    'subject of the mail:
    .Subject = "Subject"    
    
    'Chr(10) represents line feed/new line, & Chr(13) represents carriage return.
    
    ' Send text and also contents from
    '  the host workbook's worksheet range as Mail Body.
    .Body = "BODY OF EMAIL"
    
    'set importance level for the mail:
    .Importance = olImportanceHigh
    'add an attachment to the mail:
    
    'send the mail:
    
    .Display
    
    End With  
    
    'clear the object variables:
    Set applOL = Nothing
    Set miOL = Nothing
    Set recptOL = Nothing 
    
    End Sub
    

    我设置的一些变量是多余的,因为我稍微编辑了代码以保持隐私,但如果有帮助请告诉我!

    如果您想使用CC而不是密件抄送,那么只需将代码更改为:

    recptOL.Type = olcc
    

相关问题