来自Outlook的电子邮件,主题中包含Excel单元格值

时间:2016-03-03 10:09:02

标签: excel vba outlook-vba

如何从excel文件中发送包含主题中单元格值的outlook的电子邮件?

我发现每个可能的代码都可以从excel发送电子邮件,但我真的希望打开电子邮件并在主题中获取excel文件中的单元格值。

所有这一切都是从Outlook而不是从Excel完成的。谢谢。

1 个答案:

答案 0 :(得分:1)

参见下面的示例

Option Explicit
Sub Cell_Value_in_Subject()
    Dim olItem As Outlook.MailItem
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSht As Excel.Worksheet
    Dim sPath As String

    sPath = "C:\Temp\Book1.xlsx" '<- Update

'   // Excel
    Set xlApp = CreateObject("Excel.Application")
'   // Workbook
    Set xlBook = xlApp.Workbooks.Open(sPath)
'   // Sheet
    Set xlSht = xlBook.Sheets("Sheet1")

    Debug.Print xlSht.Range("A1") '<- Print Value in immediate window

'   // Create e-mail Item
    Set olItem = Application.CreateItem(olMailItem)

    With olItem
        .To = "Om3r@Email.com"
        .Subject = xlSht.Range("A1")
        .HTMLBody = xlSht.Range("A2") & " is Cell Value"
        .Display
    End With

'   // Close
    xlBook.Close SaveChanges:=True
'   // Quit
    xlApp.Quit

    '// CleanUp
    Set xlApp = Nothing
    Set xlBook = Nothing
    Set xlSht = Nothing
    Set olItem = Nothing

End Sub