0

因此,首先,我对 VBA 非常陌生,由于我收到的电子邮件数量都遵循某个模板,我正在尝试自动化数据整理,以使自己免于当前所需的所有剪切和粘贴。我看过一些以前的问题,但由于我的知识很少,答案不够具体,我无法理解。

这些电子邮件中的每一封都来自特定的电子邮件地址,并具有如下所示的标准格式:

" dd/mm/yyyy hr.min.sec

xxx xxxxxxx xxxxxxxxxxxxxxxxx xxxx xxxxxx "

我想将此信息导出或复制到 excel 2003 工作表中,以便每条单独的信息位于单行的新列中,其中每封电子邮件都是新行。我希望宏能够在特定文件夹中搜索我收到的电子邮件(因为我已经在 Outlook 中设置了一些与此电子邮件地址相关的规则),从与模板匹配的每封电子邮件中复制信息并将其粘贴到一个 Excel 工作表。然后,每次我收到新电子邮件时,信息都会添加到已创建表格的底部。

希望一切都有意义,如果您需要更多信息,请告诉我。

提前致谢。

4

2 回答 2

0

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. 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.

If you provide more specific information, I can amend the code.

于 2013-10-25T18:21:04.680 回答
0

我最近做了一些完全一样的事情,只是我把它输入了一个访问数据库而不是一个 Excel 表,但想法是一样的。出于某种原因,我无法让它按照规则运行,但无论如何我发现我可以通过手动运行的宏更好地控制它。所以使用规则将所有内容放入一个文件夹,并在该文件夹下创建一个 AlreadyProcessed 子文件夹。这是一些开始的代码:

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

从 VBA 制作 Excel 文件非常容易 - 只需阅读打开 excel 并从其他 Office 程序 VBA 制作新文档 - 您正在寻找Excel.Application. 您甚至可以在 Excel 中录制宏,手动填写信息,然后基本上将代码复制到 Outlook 中并用变量替换硬编码的信息。但是,如果您要在数千封电子邮件上运行此程序,请注意记录的宏(使用选择对象)效率低下。

于 2013-08-05T09:20:08.923 回答