简体   繁体   English

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

[英]Add a specific cell in excel to email body VBA excel

I currently have a code that will generate an email based on the coming due date. 我目前有一个代码,可以根据即将到来的日期生成电子邮件。 Below is the example of my table. 以下是我的表格示例。 [Image of Excel Table][1] [1]: https://i.stack.imgur.com/lYhlD.png . [Excel表格图片] [1] [1]: https : //i.stack.imgur.com/lYhlD.png My code works to populate an email but I want to state the ID, Description, assigned to, and due date into the email field. 我的代码可以填充电子邮件,但是我想在电子邮件字段中输入ID,说明,分配给的日期和截止日期。 Can someone help? 有人可以帮忙吗?

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

I like creating what I want in Excel, then importing it. 我喜欢在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

This sets the subject as "My email subject is to" and whatever is in A1. 这会将主题设置为“我的电子邮件主题为”,以及A1中的所有内容。

Rinse and repeat for all of the fields. 冲洗并重复所有字段。

It allows for easy editing inside of Excel without needing to go into VBA. 它允许在Excel中轻松进行编辑,而无需进入VBA。

To get the Id Number on Email body, see example 要获取电子邮件正文上的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM