简体   繁体   English

使用VBA将特定信息从Outlook 2003提取到Excel

[英]Extracting specific information from Outlook 2003 to Excel using VBA

So firstly, I'm very new to VBA and due to the number of emails I get that follow a certain template, I'm trying to automate the data collation to save myself from all the cutting and pasting that is currently required. 因此,首先,我是VBA的新手,由于我收到的遵循特定模板的电子邮件数量众多,因此我试图使数据整理自动化,以免于当前需要的所有剪切和粘贴。 I've looked at some previous questions but due to my very little knowledge, the answers aren't specific enough for me to understand. 我已经看过以前的一些问题,但是由于我的知识很少,所以答案不够具体,我无法理解。

Each one of these emails is from a particular email address and has a standard format as shown below: 这些电子邮件中的每一封均来自特定的电子邮件地址,并具有如下所示的标准格式:

" dd/mm/yyyy hr.min.sec “ dd / mm / yyyy hr.min.sec

xxx xxxxxxx xxxxxxxxxxxxxxxxx xxxx xxxxx " xxx xxxxxxx xxxxxxxxxxxxxxxxx xxxx xxxxx“

I would like to export or copy this information to an excel 2003 worksheet so that each separate piece of information is in a new column of a single row, where each email is a new row. 我想将此信息导出或复制到excel 2003工作表中,以便每条单独的信息都位于一行的新列中,其中每封电子邮件都是新行。 I would like the macro to be able to search through my received emails in a particular folder (as I've already set up some rules in outlook relating to this email address), copy the information from each email matching the template and paste it into a single excel worksheet. 我希望该宏能够在特定文件夹中搜索我收到的电子邮件(因为我已经在Outlook中设置了与此电子邮件地址相关的一些规则),从与模板匹配的每封电子邮件中复制信息并将其粘贴到一个Excel工作表。 Then each time I get a new email, the information will be added to the bottom of the table thats been created. 然后,每当我收到一封新电子邮件时,该信息将被添加到已创建表的底部。

Hopefully that all makes sense, please let me know if you need anymore information. 希望一切都有意义,如果您需要更多信息,请告诉我。

Thanks in advance. 提前致谢。

I did something exactly like this recently, except that I had it entered into an access database instead of an excel sheet, but the idea is the same. 我最近做了些完全一样的事情,除了我将它输入到访问数据库而不是Excel工作表中,但是想法是一样的。 For some reason, I was having trouble getting it to run with rules, but I anyways found that I could control it better from a manually run macro. 由于某种原因,我很难使其与规则一起运行,但是无论如何,我发现我可以通过手动运行的宏更好地控制它。 So use a rule to put everything into a folder, and make an AlreadyProcessed subfolder under that. 因此,使用规则将所有内容放入文件夹,并在该文件夹下创建一个AlreadyProcessed子文件夹。 Here is some code to start from: 这是一些代码开始于:

Sub process()
    Dim i As Integer, folder As Object, item As Object
    With Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("YourFolderName")
    For Each item In .Items
        processMail item
        item.Move .Folders("AlreadyProcessed")
    Next
    End With
End Sub

Sub processMail(item As Outlook.MailItem)
    Dim bitsOfInformation() As String
    bitsOfInformation = Split(item.Body, " ")
    'Use this information to make an Excel file
End Sub

Making Excel files from VBA are very easy - just read up on opening excel and making new documents from other Office program VBAs - you're looking for Excel.Application . 从VBA制作Excel文件非常容易-您只需打开Excel并从其他Office程序VBA制作新文档即可-您正在寻找Excel.Application You can even record a macro in Excel, filling the information manually, and basically copy the code into Outlook and replace the hard-coded information with variables. 您甚至可以在Excel中记录宏,手动填充信息,然后基本上将代码复制到Outlook中,并用变量替换硬编码的信息。 But if you're going to be running this on thousands of e-mails, be warned that recorded macros (that use selection objects) are inefficient. 但是,如果要在成千上万的电子邮件上运行此功能,请警告已录制的宏(使用选择对象)效率低下。

Start with the following code: 从以下代码开始:

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
  Set Items = GetItems(GetNS(GetOutlookApp), olFolderInbox)
End Sub

Private Sub Items_ItemAdd(ByVal item As Object)
  On Error GoTo ErrorHandler

  Dim msg As Outlook.MailItem

  If TypeName(item) = "MailItem" Then
    Set msg = item



  End If
ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub
Function GetItems(olNS As Outlook.NameSpace, folder As OlDefaultFolders) As Outlook.Items
  Set GetItems = olNS.GetDefaultFolder(folder).Items
End Function
Function GetNS(ByRef app As Outlook.Application) As Outlook.NameSpace
  Set GetNS = app.GetNamespace("MAPI")
End Function
Function GetOutlookApp() As Outlook.Application
  Set GetOutlookApp = Outlook.Application
End Function

This sets an event listener on your default Inbox. 这将在默认收件箱上设置事件侦听器。 Whenever an email message is received, the code inside the If TypeName statement will be executed. 每当收到电子邮件时, If TypeName语句中的代码将被执行。 Now it's simply a matter of what code you want to run. 现在,只需要运行什么代码即可。

You can check the sender using the .SenderName or .SenderEmailAddress properties to make sure it's the right sender. 您可以使用.SenderName.SenderEmailAddress属性检查发件人,以确保它是正确的发件人。

If you provide more specific information, I can amend the code. 如果您提供更多具体信息,我可以修改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM