在excel中将特定单元格添加到电子邮件正文VBA excel

时间:2019-02-04 21:06:35

标签: excel vba email outlook

我目前有一个代码,该代码将根据即将到来的日期生成电子邮件。以下是我的表格示例。 [Excel表的图像] [1] [1]:https://i.stack.imgur.com/lYhlD.png。我的代码可以填充电子邮件,但是我想在电子邮件字段中输入ID,说明,分配给的日期和截止日期。有人可以帮忙吗?

Sub datesexcelvba()
Dim myApp As Outlook.Application, mymail As Outlook.MailItem
Dim mydate1 As Date
Dim mydate2 As Long
Dim datetoday1 As Date
Dim datetoday2 As Long

Dim x As Long
With Sheets("Sheet2")
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lastrow

mydate1 = Cells(x, 7).Value
mydate2 = mydate1

Cells(x, 37).Value = mydate2

datetoday1 = Date
datetoday2 = datetoday1

Cells(x, 36).Value = datetoday2

If mydate2 - datetoday2 = 10 Then


Set myApp = New Outlook.Application
Set mymail = myApp.CreateItem(olMailItem)
mymail.To = Cells(x, 31).Value
mymail.CC = Cells(x, 32).Value


With mymail
.Subject = "Payment Reminder"
.Body = "Please close your ICAR  by due date"
.Display
'.send
End With

Cells(x, 33) = “Yes”
Cells(x, 33).Interior.ColorIndex = 3
Cells(x, 33).Font.ColorIndex = 2
Cells(x, 33).Font.Bold = True
Cells(x, 33).Value = mydate2 - datetoday2
End If

Next
Set myApp = Nothing
Set mymail = Nothing
End With

End Sub

2 个答案:

答案 0 :(得分:0)

我喜欢在Excel中创建所需的内容,然后将其导入。

SubjectTemp = wb.names("NamedRange!").Referstorange.value2

-

With mymail
.Subject = SubjectTemp
.Body = "Please close your ICAR  by due date"
.Display
'.send
End With

-

="My email subject is to "&$A$1

这会将主题设置为“我的电子邮件主题是”,以及A1中的所有内容。

冲洗并重复所有字段。

它允许在Excel中轻松进行编辑,而无需进入VBA。

答案 1 :(得分:0)

要获取电子邮件正文上的ID号,请参见示例

Dim x As Long
Dim Sht As Worksheet
Set Sht = ThisWorkbook.Sheets("Sheet2")

Dim lastrow As Long
With Sht
    lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row

    For x = 2 To lastrow

        .Cells(x, 37).Value = mydate2
         Set myApp = New Outlook.Application
         Set mymail = myApp.CreateItem(olMailItem)

        Dim IDnumber As String
        IDnumber = .Cells(x, 1).Value

        Debug.Print IDnumber ' print on immediate Window

        With mymail
            .Subject = "Payment Reminder"
            .Body = "ID Number " & IDnumber
            .Display
            '.send
        End With

        .Cells(x, 33) = "Yes"
        .Cells(x, 33).Interior.ColorIndex = 3
        .Cells(x, 33).Font.ColorIndex = 2
        .Cells(x, 33).Font.Bold = True
    Next