【发布时间】:2011-08-15 03:10:14
【问题描述】:
首先,我是一个从头开始工作的 VB 新手,但过去曾编辑过一些代码。我能找到的最接近我的问题是this one,但它并不像我希望的那样具体。
所以我使用的是 Outlook/Excel 2007,并且我每天都会收到一封电子邮件,其中包含一些固定形式的数据。我希望做的是设置一个宏/脚本来搜索我的 Outlook 收件箱,然后根据正确的邮件主题,查看邮件正文并将某些部分提取到 Excel 工作表中。
根据我的知识,我认为 VB 可能是最好的方法,但我不太确定从哪里开始。对代码的一般结构或其他类似示例的任何帮助将不胜感激。只是想开始,并希望自己能在以后的练习中解决这个问题。谢谢!
非常感谢您的帮助!我大部分时间都在工作,只是在收到新消息时无法让它自动更新。我设置了一个规则,将相关电子邮件移动到他们自己的文件夹中,并且我能够设置一个我可以运行的公共宏,它可以提取所有数据(对于每封电子邮件)并将它们转储到一个 .csv 文件中。
我尝试将该宏调整到您在上面发布的示例中,当我收到新消息时它应该会自动运行,但我还没有成功。电子邮件的解析不应该改变(并且肯定可以在手动运行的宏中工作),所以这很好,它只是让自动更新宏在新消息上运行。我错过了什么吗?这是我得到的,除了新文件夹(并且是一个类模块)之外,它与上面的示例基本相同:
Public WithEvents myOlItems As Outlook.Items
Public Sub Application_Startup()
' Reference the items in the Inbox. Because myOlItems is declared
' "WithEvents" the ItemAdd event will fire below.
Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Folders("FolderX").Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim objOutlook As New Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As MailItem
Dim count As Integer
Dim myTitlePos As Integer
Dim myTitleLen As Integer
Dim myVarPos As Integer
Dim myVarLen As Integer
Dim strPrice As String
Dim strYear As String
Dim myVarCRLF As Integer
Dim myDate As Date
Dim newLineTest As String
' Check to make sure it is an Outlook mail message, otherwise
' subsequent code will probably fail depending on what type
' of item it is.
If TypeName(Item) = "MailItem" Then
' Data processing and parsing is done here
End Sub
【问题讨论】: