简体   繁体   English

如何将 Outlook 2010 电子邮件中的表格传输到 Excel 2010

[英]How to transfer tables in Outlook 2010 emails to Excel 2010

I have around 2000 emails which were sent to me in a zip file.我有大约 2000 封以 zip 文件形式发送给我的电子邮件。 The emails have the following structure:电子邮件具有以下结构:

http://social.msdn.microsoft.com/Forums/getfile/429285 http://social.msdn.microsoft.com/Forums/getfile/429285

All mails have the same Subject.所有邮件都有相同的主题。 As can be seen from the screenshot, each mail has multiple tables.从截图中可以看出,每封邮件都有多个表。 These tables have varying number of rows with each mail.这些表中每封邮件的行数各不相同。 My task is to get all of these multiple tables contained in 2000 mails into excel to form graphs and charts.我的任务是将 2000 封邮件中包含的所有这些多个表都导入 excel 以形成图形和图表。 Can you please help me with how to go about this through automation.你能帮我解决这个问题吗? I tried a few solutions available but found nothing which could transfer tables within Outlook emails to Excel.我尝试了一些可用的解决方案,但没有发现可以将 Outlook 电子邮件中的表格传输到 Excel。 I have a deadline for this task and any prompt help will be really appreciated.我有这个任务的最后期限,任何及时的帮助将不胜感激。 Thanks in advance!提前致谢!

This worked for me.这对我有用。

Add a reference to Microsoft HTML Object Library In your VBA project (under Tools >> References in the VB editor)在您的 VBA 项目中添加对Microsoft HTML Object Library的引用(在 VB 编辑器中的工具 >> 引用下)

在此处输入图片说明

Assumes outlook is already open, and your mails are stored in the path MSG_PATH .假设 Outlook 已经打开,并且您的邮件存储在路径MSG_PATH

Sub Tester()

Const MSG_PATH As String = "C:\_Stuff\test\mails\"

Dim ol, m, t, r, c
Dim doc As New MSHTML.HTMLDocument
Dim rng As Range, rw As Object
Dim f

    Set ol = GetObject(, "outlook.application")

    Set rng = ActiveSheet.Range("B2")

    f = Dir(MSG_PATH & "*.msg")

    Do While Len(f) > 0

        Set m = ol.CreateItemFromTemplate(MSG_PATH & f)
        doc.body.innerHTML = m.htmlbody
        m.Close False

        For Each t In doc.getElementsByTagName("table")
            rng.Offset(0, -1).Value = f
            For r = 0 To t.Rows.Length - 1
                Set rw = t.Rows(r)
                For c = 0 To rw.Cells.Length - 1
                    'ignore any problems with merged cells etc
                    On Error Resume Next
                    rng.Offset(r, c).Value = rw.Cells(c).innerText
                    On Error GoTo 0
                Next c
            Next r
            Set rng = rng.Offset(t.Rows.Length + 5)
        Next t

        f = Dir()
    Loop

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM