vba - 在 Excel 电子表格中捕获电子邮件接收时间
问题描述
我需要从头开始。我的代码和我的手臂一样长。我“只是”想弄清楚如何获取电子邮件的发送日期并将其插入 Excel 中的特定列。我已经想出了如何在电子邮件正文中获取 HTML 表格并将其放入 Excel 中。现在,“所有”我需要做的就是捕获电子邮件的日期并放入列
Public Sub Driver()
Dim Item As MailItem, x%
Dim r As Object 'As Word.Range
Dim doc As Object 'As Word.Document
Dim xlApp As Object
Dim olItems As Outlook.Items
Dim sourceWB As Workbook
Dim sourceSH As Worksheet
Dim olFolder As Outlook.Folder
Dim strFile As String
Dim olEleColl As MSHTML.IHTMLElementCollection
Dim olNameSpace As Outlook.NameSpace
Dim olHTML As MSHTML.HTMLDocument: Set olHTML = New MSHTML.HTMLDocument
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim objEmail As Outlook.MailItem
Dim intRowIndex As Integer
Dim intEmailIndex As Integer
Dim objFolder As Outlook.MAPIFolder
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.EnableEvents = False
.EnableEvents = False
.DisplayAlerts = False
End With
Set olNameSpace = Application.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox)
Set olItems = olFolder.Items
'olItems.Sort ("[ReceivedTime]")
Set Item = olItems(olItems.Count)
'save Outlook email's html body (tables)
With olHTML
.Body.innerHTML = Item.HTMLBody
Set olEleColl = .getElementsByTagName("table")
End With
strFile = "C:\xls\Driver.xlsx"
Set sourceWB = Workbooks.Open(strFile, , False, , , , , , , True)
Set sourceSH = sourceWB.Worksheets("Sheet1")
sourceWB.Activate
cells.Select
Selection.Delete
For Each Item In Application.ActiveExplorer.Selection
Set doc = Item.GetInspector.WordEditor
For x = 1 To doc.tables.Count
Set r = doc.tables(x)
r.Range.Copy
sourceSH.Paste
ActiveSheet.Pictures.Delete
rows(4).Delete
rows(1).EntireRow.Delete
rows(1).EntireRow.Delete
rows(1).EntireRow.Delete
Range("D:E").Delete
sourceSH.cells(sourceSH.rows.Count, 1).End(3).Offset(1).Select
sourceSH.cells(1, 4) = "Received Time"
Next
Next
sourceWB.Save
sourceWB.Close
Set sourceWB = Nothing
xlApp.Quit
Set xlApp = Nothing
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
On Error Resume Next
With OutlookMail
.To = "me@memememe.com"
.CC = ""
.BCC = ""
.Subject = "If this works!"
.Body = "Test."
.Attachments.Add ("c:\xls\Driver.xlsx")
.Send
End With
Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub
解决方案
好的,我想出了一些可能不可取的东西,但它对我有用。我添加了一个 specialcells 函数,用于搜索列中的空白单元格,然后添加我需要的日期。非常感谢所有的帮助
Public Sub Driver()
Dim Item As MailItem, x%
Dim r As Object 'As Word.Range
Dim doc As Object 'As Word.Document
Dim xlApp As Object
Dim olItems As Outlook.Items
Dim sourceWB As Workbook
Dim sourceSH As Worksheet
Dim olFolder As Outlook.Folder
Dim strFile As String
Dim olEleColl As MSHTML.IHTMLElementCollection
Dim olNameSpace As Outlook.NameSpace
Dim olHTML As MSHTML.HTMLDocument: Set olHTML = New MSHTML.HTMLDocument
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim objEmail As Outlook.MailItem
Dim intRowIndex As Integer
Dim intEmailIndex As Integer
Dim objFolder As Outlook.MAPIFolder
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.EnableEvents = False
.EnableEvents = False
.DisplayAlerts = False
End With
Set olNameSpace = Application.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox)
Set olItems = olFolder.Items
'olItems.Sort ("[ReceivedTime]")
Set Item = olItems(olItems.Count)
'save Outlook email's html body (tables)
With olHTML
.Body.innerHTML = Item.HTMLBody
Set olEleColl = .getElementsByTagName("table")
End With
strFile = "C:\xls\Driver.xlsx"
Set sourceWB = Workbooks.Open(strFile, , False, , , , , , , True)
Set sourceSH = sourceWB.Worksheets("Sheet1")
sourceWB.Activate
cells.Select
Selection.Delete
For Each Item In Application.ActiveExplorer.Selection
Set doc = Item.GetInspector.WordEditor
For x = 1 To doc.tables.Count
Set r = doc.tables(x)
r.Range.Copy
sourceSH.Paste
ActiveSheet.Pictures.Delete
rows(4).Delete
rows(1).EntireRow.Delete
rows(1).EntireRow.Delete
rows(1).EntireRow.Delete
Range("D:E").Delete
sourceSH.cells(sourceSH.rows.Count, 1).End(3).Offset(1).Select
sourceSH.cells(2, 4) = Item.ReceivedTime
sourceSH.cells(1, 4) = "Received Time"
Range("D2").CurrentRegion.SpecialCells(xlCellTypeBlanks).Value = Item.ReceivedTime
Next
Next
End Sub
推荐阅读
- apache-pig - 当我们使用条件时如何处理展平运算符
- encoding - WebStorm 终端编码/字体问题
- javascript - JS - 在加载其他资源之前设置 Base-Path
- java - 为什么谷歌日历事件 API 不支持 IST 时区?
- r - 带有 Metropolis 主题的 rmarkdown 幻灯片中的字体问题
- codeigniter - 按日期搜索在分页中不起作用
- java - Firestore:查询所有集合 orderBy 和 startsWith 最后 1 个结果
- flutter - Flutter Dart 尝试捕获异常行号
- c# - Xamarin 表单和 Azure 认知服务:语音转文本服务不起作用
- reactjs - Babel 5 插件正在使用不受支持的 Babel 版本运行。试图更新 babel-relay-plugin