可以在Outlook中读取电子邮件并将某些数据导出到Excel表格中吗?

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

电子邮件主题:“属性报告” (不属于电子邮件正文部分,第一行是名称:段落之间没有空行)

名称: 地理。

开始时间:03: 10 PM 10-Aug-2014

结束时间:03:11 PM 10-Aug-2014

物业: 48大西洋closures

完整的清单: 没有

呼唤: 是的

圣人n°: 16

完成的工作:

作业1。 工作1 之后,可能会有更多(job2,job3等),但我会在稍后处理

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

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

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

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

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

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

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

date到列A,名称到列B,鼠尾到列D,完整的清单列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 

是的,Outlook可以处理传入的电子邮件,然后您可以使用这些数据来处理VBA所能做的任何事情,比如将数据放入Excel电子表格,写出纯文本文件等。

您可以使用NewMail的应用程序级别的事件过程, NewMail收到新的电子邮件时都会引发(我相信这会忽略其他项目types,如日历约会,任务等,您可以使用NewMailEx事件处理所有传入项目)。

NewMailEx事件接收与每个邮件项关联的逗号分隔的唯一IDstring。 使用简单的Splitfunction将其转换为可迭代的数组。

然后将Excel绑定到Outlook,创build一个新的电子表格,并input您需要的任何信息。 这个例子使用了早期的绑定,它需要对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 

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

  • 你想使用正则expression式吗?
  • 或者你可以使用简单的string函数, LeftMid
  • Outlook还支持使用WordEditor和Word的富文本对象模型来parsing段落等。

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

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

如果您在实现时遇到了特定的问题,parsing所需的信息等,我鼓励您在浏览“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

注意此代码未经testing ,可能包含一些错别字,错位的括号。 我鼓励你总是声明variables,并使用Option Explicit在模块中强制variables声明。

注意这个例子将为每个电子邮件创build一个新文件,可能不需要。 为了挖掘现有的文件,你需要知道它的位置,并能够打开它。 Set xlWb = xlApp.Workbooks.Add ,而不是设置xlWB = xlApp.Workbooks.Open(“c:\ path \ to \ file.xlsx”)。 使用现有文件为不熟练VBA的人创build了一系列额外的“问题”,例如“如何在工作表中find下一个空行”(这里提及并回答了几十次)等等。

所以我会从这个开始,很明显,如果你不能“得到它”,当你卡住时,把它分解成小块或在这个过程中的步骤。 在试图把它们放在一起之前,试着弄清楚如何做每一步。

祝你好运!

(我星期六晚上应该有更好的事情,但是我有一个2岁的老婆,还有妻子出城…)