使用 VB/VBA 搜索 Outlook 邮件并将特定数据提取到 Excel 工作表中

编程入门 行业动态 更新时间:2024-10-09 08:27:33
本文介绍了使用 VB/VBA 搜索 Outlook 邮件并将特定数据提取到 Excel 工作表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是一名 VB 新手,从头开始工作,但过去曾编辑过一些代码.我能找到的最接近的问题是 这个 但它并不像我希望的那么具体.

So first things first, I'm a VB newbie working from scratch but have edited some code in the past. The closest question I could find to mine was this one but it wasn't quite as specific as I hoped.

所以我使用的是 Outlook/Excel 2007,我每天都会收到一封电子邮件,其中包含一些固定格式的数据.我希望做的是设置一个宏/脚本来搜索我的 Outlook 收件箱,然后根据正确的邮件主题,查看邮件正文并将某些部分提取到 Excel 工作表中.

So I'm using Outlook/Excel 2007 and I receive a daily email that contains some data in a fixed form. What I am hoping to do is set up a Macro/Script that will search my Outlook Inbox, and then based on the correct Message Subject, will look in the body of the message and extract certain portions into an Excel worksheet.

根据我的知识,我认为 VB 可能是最好的方法,但我不太确定从哪里开始.对代码的一般结构或其他类似示例的任何帮助将不胜感激.只是想开始,并希望自己弄清楚以备将来的练习.谢谢!

I think VB is probably the best way to do this based on my knowledge, but I'm not quite sure where to start. Any help on the general structure of the code or other similar examples would be much appreciated. Just looking to get started and hopefully figure it out on my own for future exercises. Thanks!

非常感谢您的帮助!我大部分时间都在工作,只是无法在收到新消息时让它自动更新.我设置了一个规则,将相关电子邮件移动到他们自己的文件夹中,并且我能够设置一个公共宏,我可以运行该宏来提取所有数据(对于每封电子邮件)并将它们转储到 .csv 文件中.

So thanks so much for the help! I've mostly got this working, I just haven't been able to get it to automatically update when I get a new message. I have a rule set up that moves the relevant emails into their own folder, and I was able to set up a public macro that I can run that pulls all the data out (for every email) and dumps them into a .csv file.

我尝试将该宏应用到您上面发布的示例中,该示例在我收到新消息时应自动运行,但我还没有成功.电子邮件的解析不应该改变(并且肯定适用于手动运行的宏),所以没关系,它只是让自动更新宏在新邮件上运行.我错过了什么吗?这是我得到的,除了新文件夹(并且是一个类模块)之外,它与上面的示例基本相同:

I tried to adapt that macro into the example you posted above that should automatically run when I receive a new message, but I haven't succeeded yet. The parse-ing of the emails shouldn't change (and definitely works in the manually run macro), so that is fine, it's just getting the auto-update macro to run on a new message. Am I missing something? Here is what I've got, which is basically the same as the example above aside from the new folder (and is a class module):

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

推荐答案

VB 可能是解决您的问题的最简单的语言,因为您对这一切都不熟悉,而 VBA(Visual Basic for Applications)是最简单和最具有互操作性的语言特定问题的语言.

VB is probably the easiest language to work with for your problem since you are new to all this and VBA (Visual Basic for Applications) is the simplest and most interoperable language for the particular problem.

首先,您需要创建一个新的 Outlook 宏,每当有新邮件到达您的收件箱时都会触发该宏.

You'll want to start by creating a new Outlook macro that fires whenever a new mail arrives in your inbox.

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

Start by creating a new class module in Outlook (ALT-F11) and copy in the following code:

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 对象库的引用.

The next part is to open Excel and do whatever stuff it is you want to do. Be sure to establish the reference to the excel object library by using "Tools:References..." menu item and selecting Microsoft Excel xx.xx object library.

您可能需要如下代码:

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 方法中调用.

This would likely be called from within your myOlItems_ItemAdd method.

一些在 Google 或 Stack Overflow 上环顾四周的人应该会给您足够的指示,说明您可能希望如何处理 Excel 方法的实际数据处理部分.

Some looking around on Google or Stack Overflow should give you enough pointers about how you might want to handle the actual data processing part for your Excel method.

希望这足以让您入门.

这篇关于使用 VB/VBA 搜索 Outlook 邮件并将特定数据提取到 Excel 工作表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-29 12:39:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/757006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将   邮件   数据   工作   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!