2016-09-22 141 views
0

我通過VBA Outlook.Application參考從Excel創建電子郵件。每封電子郵件都填充了我的Excel表中的數據,然後放入To/CC/BCC/Subject/Body字段。Office 2013 - VBA電子郵件不顯示到/ CC /密件抄送變量

現在,在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 
+0

首先添加該代碼或調用此子,沒有一個空字符串添加到回覆收件人。其次,如果您使用Recipients.Add而不是設置To/CC/BCC屬性,請使用不同的結果嗎? –

回答

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 
+0

我還沒有使用Office 2016,但我會記住,謝謝。 – Armitage2k

0

而不是創建一個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 = "[email protected]" 
    oMailItem.CC = "" 
    oMailItem.BCC = "[email protected]" 
    oMailItem.Subject = Sheet1.Cells(15, "D") 
    oMailItem.HTMLBody = "Again .. testing" 
    oMailItem.Display 

    Set oMailItem = Nothing 
    Set oOlApp = Nothing 

End Sub 

您可以在您的子從子帶參數

+0

我知道你來自哪裏,但我可能必須在用戶無法激活引用的機器上部署此應用程序。目前,我正在檢查Outlook在執行此代碼之前是否已打開並正在運行。任何需要手動激活參考的方法? – Armitage2k

+0

我剛剛嘗試過類似於你的代碼,它在Office Professional Plus 2013中爲我工作。我有一個'Class Module'中的代碼,當我初始化該類時,檢查Outlook是否打開。你有哪個版本的Office 2013? – Zac

相關問題