简体   繁体   中英

Outlook VBA run a script is running before specified email moved to the target folder

I have an outlook vba code to save the table from a daily email that i receive. I creted a rule to move this email to a specific folder and run a vba script to save the table from this new emails body. However, when emai is received, vba starts running and grabbing previous email with the same subject and only after macro is done a new email (that i actually need to be grabbed) is appeared as new in my target folder and the email's body that was saved to excel is a data from previous email. I tried sleep, still doesn't work. Is it anyway to let outlook first move new email to a target folder and only after run a script to get the newest received unread email.

Here is my code for reference:

Sub ExportOutlookTableToExcel()`

Dim oLookInspector As Inspector
Dim oLookMailitem As MailItem

Dim oLookWordDoc As Word.Document
Dim oLookWordTbl As Word.Table

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook 
Dim xlWrkSheet As Excel.Worksheet


'Grab Email Item
 Set oLookMailitem =Application.ActiveExplorer.CurrentFolder.Items("Apples Sales")

 Set oLookInspector = oLookMailitem.GetInspector

 Set oLookWordDoc = oLookInspector.WordEditor

Re: I created a rule to move this email to a specific folder and run a VBA script
You are not the first to fall into this trap. Put the move as the last action in the code.
Consider not using "run a script" code in rules. There is ItemAdd for any folder or NewMailEx for the Inbox.

Re: Set oLookMailitem =Application.ActiveExplorer.CurrentFolder.Items("Apples Sales")
The most recent mail with subject "Apples Sales" can be found like this:

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Sub mostRecentlyReceivedMail_Subject()

Dim oLookFolder As Folder
Dim oLookFolderItems As Items

Dim srchSubject As String
Dim i As Long

Dim oLookMailitem As MailItem

Set oLookFolder = ActiveExplorer.CurrentFolder
Set oLookFolderItems = oLookFolder.Items

' sort the collection not the folder
oLookFolderItems.Sort "[ReceivedTime]", True

srchSubject = "Apples Sales"

For i = 1 To oLookFolderItems.Count

    ' first verify object in folder is a mailitem
    If oLookFolderItems(i).Class = olMail Then
    
        Set oLookMailitem = oLookFolderItems(i)
        
        If oLookMailitem.subject = srchSubject Then
            Debug.Print oLookMailitem.ReceivedTime
            oLookMailitem.Display
            Exit For
        End If
    End If
    
Next

End Sub

I creted a rule to move this email to a specific folder and run a vba script to save the table from this new emails body.

There is no need to create a rule and run a VBA script. Instead, to handle incoming emails immediately you need to handle the NewMailEx event which is fired when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance. However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously. You should not assume that after these events fire, you will always get a one-item increase in the number of items in the Inbox. Also you may consider handling the ItemAdd event on the folder where your items are moved. But it has a known disadvantage - the event is not fired if more than sixteen items are moved at the same time. This is a known issue when dealing with OOM.

In the NewMailEx event handler you may get an instance of the incoming email and move it to the required folder programmatically where you could run any other actions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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