【发布时间】: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”中显示为主题的方式,没有空格。