【问题标题】:Add a specific cell in excel to email body VBA excel将excel中的特定单元格添加到电子邮件正文VBA excel
【发布时间】:2019-02-05 15:19:20
【问题描述】:

我目前有一个代码,可以根据即将到来的截止日期生成一封电子邮件。下面是我的表的例子。 [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

【问题讨论】:

    标签: excel vba email outlook


    【解决方案1】:

    要获取电子邮件正文上的 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
    

    【讨论】:

      【解决方案2】:

      我喜欢在 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。

      【讨论】:

      • 您好,感谢您的回复。我不明白这一点,我有一个特定的单元格,如果这是真的,那么它将发送一封电子邮件。在电子邮件中,我希望将真实陈述中的某些单元格填充到电子邮件正文中。
      • 好的。我正在使用命名范围将数据存储在 Excel 中。实际数据来自公式和您在 Excel 中键入的内容。从那里,我使用第一行,subjecttemp 将数据导入 Excel。然后我使用我提取的数据来填写电子邮件主题字段。这可以对身体、附件等重复。
      • 你好,我还是不明白...你能试着把它放在我的代码中至少 ID 号吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多