此处添加两个前期绑定,
Microsoft Outlook 16.0 Object Library
Microsoft Word 16.0 Object Library
此处为什么要对Word进行前期绑定引用呢?因为邮件的正文其实是一个Word文档页面。我们可以像写入Word那样进行书写邮件,如果你会Word VBA,那么今天这个问题就很简单啦。
二、代码展示
Sub DownloadTables()
Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim olInspector As Outlook.Inspector
Dim olDocument As Word.Document
Dim tb As Word.Table, arr()
Dim sht As Worksheet
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox)
For Each olMail In olFolder.Items
olMail.Display
Set olInspector = olMail.GetInspector
Set olDocument = olInspector.WordEditor
If olDocument.Tables.Count > 0 Then
Set sht = ThisWorkbook.Sheets.Add
sht.Name = Replace(Replace(olMail.Subject, "回复:", ""), "转发:", "")
For Each tb In olDocument.Tables
ReDim arr(1 To tb.Rows.Count, 1 To Columns.Count)
For i = 1 To tb.Rows.Count
For j = 1 To tb.Columns.Count
arr(i, j) = tb.Cell(i, j).Range.Text
Next j
Next i
sht.Range("A65535").End(xlUp).Offset(2, 0).Resize(UBound(arr), UBound(arr, 2)).Value = arr
Next
End If
olMail.Close olDiscard
Next olMail
End Sub
好了,本期教程就到这里啦,走过路过的点个关注分享一下吧,谢谢啦,如果你是一个VBA小白,想要了解该如何开始学习VBA,建议看看以下文章
Excel VBA学习路线知识框架梳理(小白VBA入门必看-建议收藏)