Extracting specific information from Outlook 2003

2019-09-04 12:43发布

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

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

2条回答
你好瞎i
2楼-- · 2019-09-04 13:05

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

查看更多
看我几分像从前
3楼-- · 2019-09-04 13:14

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.

查看更多
登录 后发表回答