VBA Outlook 2010.将特定的Outlook正文文本解压到excel

有几个post打开这个话题,但似乎没有完全做我所需要的。 我做了一些编程,但是我从来没有用过VBA。 每天我收到一系列包含以[Tk#*******]新请求(* = 7位数字)开始的主题行的一系列电子邮件,内容为10到50个,然后在正文中有一个表单看起来像这样:

主体

然后,我在Excel中设置了'用户名','G''公司','H'和'I'中的'Ticket'。 我想首先从主题中提取7位数的票号,并​​把它放在excel的“Ticket”字段中,然后从login字段中将“smithjoh”部分放在“用户名”中,然后将外部公司列入'公司',最后到期date为'有效期至'。

首先,我想知道这是否可能,因为它是提取数据的特定部分,如果有的话,如果有人可以帮助我做这个工作,将不胜感激。 我试图自己做,但是缺乏经验让我无能为力,不幸的是没有任何工作。 如果能这样做,它会帮助我很多,因为它会自动执行一个非常繁琐的手动任务。

谢谢,马克

好吧,如果你抓住身体和标题没有问题,那么你只需要做string操作。 将它们分别读入一个variables。 假设你的主题是litterally“TK#*******新要求”,那么这应该让你开始。 基本上重复这个逻辑你想要拉出的每个部分。 一旦你有variables的值,你可以把这些值放入单元格。

Dim sSubject as string Dim sBody as string Dim sTicket as string Dim sLogin as string Dim lLoginPos as long Dim iLoginLength as integer sTicket = Mid(sSubject,4,7) 'start 4th char and grab 7 ... if you actually have the brackets in the subject then do 5,7 lLoginPos = InStr(sBody, "emea") + 5 'if they aren't all emea you will need to key off the "Req on behalf name" and add more to the result the result will be the e in emea so add 5 to get past that to the login. iLoginLength = InStr(sBody, "Req on behalf mail") - lLoginPos ' you might need to subtract an additional 1 or 2 if your result isn't perfect. Not sure what the blank white spaces in your screenshot will do here. sLogin = Mid(sBody,lLoginPos,iLoginLength) ' grabs that username. With Sheets("Sheet1") cells(1,3) = sLogin ' puts that into Row 1 Colum 3 (C) Cells is R1C1 ... backwards from the normal way you think of excel End With 

使用其他stringvariables你想抓取的所有其他部分,只需重复查找起始位置和结束位置的逻辑,然后使用中间将其放入一个variables。

给它一个填补其余的,如果你卡住,发表你的代码到这一点,让我们知道什么线路给你的麻烦。 不要忘记打开你的本地窗口,并使用F8键逐行扫描一行代码,这样就可以看到到底发生了什么。 这对string操作特别有用。