Outlook可以读取电子邮件将某些数据导出到Excel工作表吗?

时间:2014-08-10 00:07:02

标签: excel vba email excel-vba

确定。我编辑了这个问题,看看你是否能看出我犯了什么错误。 这是电子邮件。除了在Job1之后,它将总是以这种结构出现。可能还有六条线。但我稍后会处理。 这是电子邮件:

电子邮件主题:“财产报告”(不属于电子邮件正文。第1行是姓名:。段落之间没有空行。)

姓名: geo。

时间开始:03:10 PM 2014年8月10日

时间结束:2014年8月10日下午03:11

物业: 48 Atlantic Close

完整清单:

喊出:

Sagenº: 16

完成工作:

Job1。 working1 在这个段落之后可能会有更多(job2,job3等)但我稍后会处理

*项目:。价格:0£。库存:0£

*项目:。价格:0£。库存:0£

*项目:。价格:0£。库存:0£

*项目:。价格:0£。库存:0£

*项目:。价格:0£。库存:0£

*项目:。价格:0£。库存:0£

所有粗体字段都是我需要导入到Excel文件的字段。

列A的日期,列B的名称,列D的sage,列F的完整清单和列G的作业。 这是你给我的VBA代码,我试着像这样编辑它:

       Sub Application_NewMailEx(ByVal EntryIDCollection As String)
      'Excel objects
      Dim xlApp As Excel.Application
      Dim xlWB As Excel.Workbook
      Dim xlSheet As Excel.Worksheet


      Dim id As Variant  'used to iterate the EntryIDCollection
      Dim email As Outlook.MailItem  'represents each email item
      Dim msgText As Variant 'Array used to iterate the "lines" in the email:

      'Create an instance of Excel that we can use:
      Set xlApp = CreateObject("Excel.Application")


      For Each id In Split(EntryIDCollection, ",")
      'Assign a mailItem variable to this email:
      Set email = Application.Session.GetItemFromID(id)

     'Add some logic to ensure you only process the right emails.
     ' you could use a defined subject and/or sender name, etc.
     ' MODIFY AS NEEDED
     If email.Subject = "Report of Property" Then

         'Ignore the HTML format, just use the "Body". Parsing HTML can be a pain
         ' in the a$$ and it is *probably* not needed here since you control the
         ' format of the email anyways.

         'This example simply prints the entire email contents in an Excel sheet
         'Add a new workbook
         Set xlWB = xlApp.Workbooks.Add
         Set xlSheet = xlWB.Worksheets(1)

         Dim line As Variant
         For Each line In Split(email.Body, vbCrLf)
              If Left(line, 1) = "Name:" Then
                  xlSheet.Range("B6").Value = Trim(Mid(line, 6))
              ElseIf Left(line, 2) = "Time started:" Then
                  xlSheet.Range("A6").Value = DateValue(Trim(Mid(line, 14)))
              ElseIf Left(line, 7) = "Sage Nº:" Then
                  xlSheet.Range("D6").Value = Trim(Mid(line, 6))
              ElseIf Left(line, 5) = "Complete Checklist:" Then
                  xlSheet.Range("F6").Value = Trim(Mid(line, 6))
              ElseIf Left(line, 5) = "Job1" Then
                  xlSheet.Range("G6").Value = Trim(Mid(line, 6))

              End If
         Next


     Else

     End If
 Next
 End Sub

1 个答案:

答案 0 :(得分:2)

是的,Outlook可以处理传入的电子邮件,然后您可以使用该数据执行VBA能够执行的任何操作,例如将数据放入Excel电子表格,写入纯文本文件等。

您可以使用NewMail的应用级事件过程,该过程会在收到新的电子邮件项目时生成(我相信这会忽略其他项目类型,例如日历约会,任务等,您可以使用处理所有传入项目的NewMailEx事件。

NewMailEx事件会收到与每个邮件项关联的逗号分隔的唯一ID字符串。使用简单的Split函数将其转换为可迭代的数组。

然后将Excel绑定到Outlook,创建新的电子表格,并输入您需要的任何信息。此示例使用早期绑定,这需要引用Excel库。

Sub Application_NewMailEx(ByVal EntryIDCollection As String)
'Excel objects
Dim xlApp as Excel.Application
Dim xlWB as Excel.Workbook
Dim xlSheet as Excel.Worksheet

MsgBox "Mail received!" '## DELETE THIS LINE ONCE YOU VERIFY THAT THE MACRO RUNS 

Dim id as Variant  'used to iterate the EntryIDCollection
Dim email as Outlook.MailItem  'represents each email item 
Dim msgText as Variant 'Array used to iterate the "lines" in the email:

'Create an instance of Excel that we can use:
Set xlApp = CreateObject("Excel.Application")


For each id in Split(EntryIDCollection, ",")
    'Assign a mailItem variable to this email:   
    Set email = Application.Session.GetItemFromID(id)

    'Add some logic to ensure you only process the right emails.  
    ' you could use a defined subject and/or sender name, etc.
    ' MODIFY AS NEEDED
    If email.Subject = "PROCESS THIS EMAIL" Then 

        'Ignore the HTML format, just use the "Body". Parsing HTML can be a pain
        ' in the a$$ and it is *probably* not needed here since you control the 
        ' format of the email anyways.

        'This example simply prints the entire email contents in an Excel sheet
        'Add a new workbook
        Set xlWb = xlApp.Workbooks.Add
        Set xlSheet = xlWB.Worksheets(1)

        'Put the email contents in the worksheet:
        xlSheet.Range("A1").Value = email.Body

        'Or you could do something like this, MODIFY AS NEEDED:
        'dim line as Variant
        'For each line in Split(email.Body, vbCrLf)
        '     If Left(line, 5) = "Name:" Then
        '         xlSheet.Range("B1").Value = Trim(Mid(line, 6))
        '     ElseIf Left(line, 13) = "Time started:" Then
        '         xlSheet.Range("C1").Value = DateValue(Trim(Mid(line, 14)))
        '     EndIf 
        'Next


    Else:
         ' do nothing for emails that don't need to be processed
    End If

    xlApp.Visible = True

Next
End Sub

实际上,从电子邮件中提取所有这些信息有很多不同的方式可以完成,我向您展示了一个简单的示例,一些更可靠(并且比其他更复杂)。

  • 您想使用正则表达式吗?
  • 或者您可以使用简单的字符串函数,例如LeftMid, 等
  • Outlook还支持使用WordEditor和Word的富文本对象模型按段落等进行解析...

任何这些或其他方法的具体实现似乎超出了这个问题的范围,我收集的是:“如何将文本从Outlook电子邮件导出到Excel spreadhseet?

这应该足以让你开始我希望:)

如果您在实现它时遇到特定问题,解析所需信息等,我建议您在浏览Outlook对象模型参考之前询问其他(新)问题,而不是:

http://msdn.microsoft.com/en-us/library/office/ff870566(v=office.14).aspx

Excel的类似参考:

http://msdn.microsoft.com/en-us/library/office/ff846392(v=office.14).aspx

注意提供的此代码未经过测试,可能包含一些拼写错误,错误的括号。我鼓励您始终声明变量并使用Option Explicit强制模块中的变量声明。

注意此示例将为每封电子邮件创建一个新文件,可能不需要。要点按现有文件,您需要知道它的位置并能够打开它。而不是Set xlWb = xlApp.Workbooks.Add,请设置`xlWB = xlApp.Workbooks.Open(“c:\ path \ to \ file.xlsx”)。使用现有文件会为不熟悉VBA的人创建一系列额外的“问题”,例如“我如何找到工作表中的下一个空行”(此处已被问及此处回答了几十次)等。

所以我会从这开始,显然如果你不能“得到它”,当你遇到困难时,将其分解为小块或者过程中的步骤。在尝试将它们放在一起之前,请尝试并弄清楚如何执行每个步骤。

祝你好运!

(星期六晚上我应该做更好的事情,但我有一个2岁的孩子,而且妻子不在城里......)

相关问题