【问题标题】:Retrieve e-mail body content depending on subject and cell value in a column根据列中的主题和单元格值检索电子邮件正文内容
【发布时间】:2021-08-09 15:27:30
【问题描述】:

我想检索具有特定主题的电子邮件的内容,该主题链接到不同列中的单元格值。

If Outlook Subject and Date Received 中的代码适用于范围例外。
而不是一个单元格值(例如 A1)我想从完整的 A 列中检索。因此对于 A 列中的每个值(在本例中为日期)包含作为主题的电子邮件的内容“总是相同的标题”和“A列单元格的日期”。

示例
A1 = 16/08/2019 ==> 电子邮件主题 = 16/08 标题 ==> B2 = 所述电子邮件的内容
A2 = 20/08/2019 ==> 电子邮件主题 = 20/08 标题 ==> B2 = 所述电子邮件的内容

Sub GetFromInbox ()

Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim olItms As Outlook.Items
Dim olMail As Variant 
Dim i As Long

Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = OlFldr.Items

olItms.Sort "Subject"
i =1

For Each olMail In olItms
    If InStr (1, olMail.Subject, "Subject" & Range ("A1") > 0 Then 
        ThisWorkbook.Sheets("Sheet1").Cells(i,2).Value = olMail.Body
        i = i + 1
     End If
 Next olMail

 Set olFldr = Nothing
 Set olNs = Nothing
 Set olApp = Nothing

 End Sub

我尝试将 Range ("A1") 更改为 range ("A:A")。

这给了

运行时错误 13:类型不匹配

我尝试了不同的方法来抵消。

Sub GetFromInbox ()

Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim olItms As Outlook.Items
Dim olMail As Variant 
Dim i As Long

Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = OlFldr.Items

olItms.Sort "Subject"
i =1

For Each olMail In olItms
    If InStr (1, olMail.Subject, "Subject" & Range ("A1") > 0 Then 
        ThisWorkbook.Sheets("Sheet1").Cells(i,2).Value = olMail.Body
        i = i + 1
    End If
 Next olMail

 Set olFldr = Nothing
 Set olNs = Nothing
 Set olApp = Nothing

 End Sub

【问题讨论】:

  • 文本 "Subject" 是否出现在电子邮件的主题中? (即“Subject16/08/2019”)。 注意:日期和文本之间没有空格。这是Subject中文本的显示方式吗?
  • 嗨 Zac,是的,这就是文本在电子邮件“Subject16/08/2019”中显示为主题的方式,没有空格。

标签: excel vba email outlook


【解决方案1】:

创建一个循环遍历 A 列的所有行的 for 循环。

LastRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row 
'Finding the last row in Column A    
For Each olMail In olItms
    For j = 1 To LastRow
      If InStr (1, olMail.Subject, "Subject" & Range ("A" & j) > 0 Then 
         ThisWorkbook.Sheets("Sheet1").Cells(i,2).Value = olMail.Body
         i = i + 1
      End If
    Next j
Next olMail

【讨论】:

  • 出于性能原因,我会从字典中的 a 列中捕获值,然后有一个循环来检查 1 个循环中的所有电子邮件
  • 不幸的是,我现在得到两个原始代码的语法错误,作为以下 'If InStr (1, olMail.Subject, "Subject" & Range ("A" & j) > 0那么'。有谁知道是什么原因造成的?
猜你喜欢
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
相关资源
最近更新 更多