【问题标题】:Using VB/VBA to search Outlook messages and extract specific data into Excel worksheet使用 VB/VBA 搜索 Outlook 邮件并将特定数据提取到 Excel 工作表中
【发布时间】: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

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    VB 可能是解决您的问题最容易使用的语言,因为您是新手,而 VBA(Visual Basic for Applications)是解决特定问题的最简单和最具互操作性的语言。

    您首先要创建一个新的 Outlook 宏,该宏会在新邮件到达您的收件箱时触发。

    首先在 Outlook (ALT-F11) 中创建一个新的类模块并复制以下代码:

    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).Items
    
    End Sub
    
    
    Private Sub myOlItems_ItemAdd(ByVal Item As Object)
    
          ' 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
    
            If Item.Subject = "My Required Subject Line" Then
    
            ' Here's where you want to do some stuff.
    
            End If
    
          End If
    
    
    End Sub
    

    下一部分是打开 Excel 并执行您想做的任何事情。请务必使用“工具:参考...”菜单项并选择 Microsoft Excel xx.xx 对象库来建立对 excel 对象库的引用。

    您可能需要如下代码:

    Private Sub Do_Excel_Stuff(MyContent As Object)
    Dim myXLApp As Excel.Application
    Dim myXLWB As Excel.Workbook
    
        Set myXLApp = New Excel.Application
        Set myXLWB = New Excel.Workbook
    
    
        ' Do your data processing here
    
    
        Set myXLWB = Nothing
        Set myXLApp = Nothing
    
    
    End Sub
    

    这可能会在您的 myOlItems_ItemAdd 方法中调用。

    Google 或 Stack Overflow 上的一些人应该可以为您提供足够的指导,让您了解如何处理 Excel 方法的实际数据处理部分。

    希望这足以让您入门。

    【讨论】:

    • 嗨 - 感谢您提供此信息。我对 Outlook 中的 VBA 非常陌生(在 Excel 中使用 VBA 大约 6 个月)。我注意到您引用了诸如 TypeName(Item) 和 (olFolderInbox) 之类的词。这些是 Outlook 原生的还是您假设我们应该定义它们?我想我对它们来自哪里以及我应该如何知道如何使用它们感到困惑。
    • 这些是 Outlook 的本机枚举或属性。如果您在 Excel 中使用这些库,则需要确保引用了正确的库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2013-04-11
    相关资源
    最近更新 更多