0

In Outlook 2010 I have thousand email products updates for multiple customers with URL in message body like that:

http://shop.khlynov.net/products/en/PRODUCT_ID_VARIABLE/enter.asp?z=UNIQUE_ACCESS_KEY

something like that:

http://shop.khlynov.net/products/en/VOP08011316314153US/enter.asp?z=AFE38DC1F69084D0B95648B21B8F1DC65E2D7E9A11A710590C60AA49390E2DC928

where:

  • all before VOP08011316314153US - constant part of URL
  • VOP08011316314153US/ - product ID variable (there are thousands)
  • enter.asp?z=AFE38DC1F69084D0B95648B21B8F1DC65E2D7E9A11A710590C60AA49390E2DC928 - access key unique for each customer (I don't use it)

I want that a script:

  1. search for PRODUCT_ID_VARIABLE in all messages in Outlook Inbox folder
  2. create subfolders named according to the PRODUCT_ID_VARIABLE (if it doesn't exist)
  3. move messages with different PRODUCT_ID_VARIABLE into corresponding subfolders.

In the example below a script should create folders VOP08011316314153US and VOP08011316314154US (if they don't already exist) and move there all messages with product IDs VOP08011316314153US and VOP08011316314154US in URLs:

Here's an example of how the email body may look like:

<table align="left">
    <tr>
        <td style="padding: 9px;" align="left">
            <p style="font-size: 10px; font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;
                            color: #333333;">
               <span style="color: #9B0124;">PRODUCT LINK: </span><br />
                  <a href="http://shop.khlynov.net/products/en/VOP23011304005259US/enter.asp?z=ABCC226C7CBA08F2D0CE2BAB7CBFE493E04D9533489C3FF245EB4061D0FA6A7D18" target="_blank" style="text-decoration: none; color: #333333;">http:/<wbr>/<wbr>shop.khlynov.net/<wbr>products/<wbr>en/<wbr>VOP23011304005259US/<wbr>enter.asp?z=ABCC226C7CBA08F2D0CE2BAB7CBFE493E04D9533489C3FF245EB4061D0FA6A7D18</a>
           </p>
       </td>
   </tr>
</table>


INBOX
-VOP08011316314153US
-- Email 1
-- Email 2
-- Email ...
-- Email X
-VOP08011316314154US
-- Email 1
-- Email 2
-- Email ...
-- Email X

I am very new in VBA coding. Could anyone help to write a code from a scratch?


I've just found that your macro work well with plain text but doesn't work with HTML letters. Here's the part of HTML code:

<table align="left">
                <tr>
                    <td style="padding: 9px;" align="left">
                        <p style="font-size: 10px; font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;
                            color: #333333;">
                            <span style="color: #9B0124;">PRODUCT LINK: </span>
                            <br />
                            <a href="http://shop.khlynov.net/products/en/VOP23011304005259US/enter.asp?z=ABCC226C7CBA08F2D0CE2BAB7CBFE493E04D9533489C3FF245EB4061D0FA6A7D18" target="_blank" style="text-decoration: none; color: #333333;">http:/<wbr>/<wbr>shop.khlynov.net/<wbr>products/<wbr>en/<wbr>VOP23011304005259US/<wbr>enter.asp?z=ABCC226C7CBA08F2D0CE2BAB7CBFE493E04D9533489C3FF245EB4061D0FA6A7D18</a>
                        </p>
                    </td>
                </tr>
            </table>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Hi Sergey, welcome to Stackoverflow. I think there are two approach to this problem. 1. is your approach, do everything in VBA. [search in Inbox](http://stackoverflow.com/questions/6105064/outlook-vba-default-folder), [create folder and move mailItem](http://superuser.com/questions/489124/create-new-folder-for-new-sender-name-and-move-message-into-new-folder) , [REGEX](http://stackoverflow.com/questions/3865500/regular-expression-rules-in-outlook-2007) 2. Create a Rule in Outlook on Incoming email --> look for specific word in body of mail--> run a script --> those in Method 1 – Larry Jan 23 '13 at 05:50
  • Thanks, VMAtm! I am very new in VBA. Could you please help me with a code? – Sergey Russkikh Jan 23 '13 at 06:59
  • 1
    It's urgent need! I would even donate some money into your PayPal account if you help me to write the code! – Sergey Russkikh Jan 23 '13 at 08:29
  • and the mailBody ONLY contains the URL? – Larry Jan 23 '13 at 09:19
  • 1
    Message bodies contain HTML with some text, URLs with different patterns, and 2 identical product update URLs with the pattern I quoted above: one as graphic button and another as text. I also noticed that the patterns of product IDs always have VOP in the beginning and US at the end divided with 14 numbers only such as: VOP??????????????US – Sergey Russkikh Jan 23 '13 at 09:48
  • I found simple macro that finds predefined sender name and moves moves all the item into predefined folder: – Sergey Russkikh Jan 23 '13 at 10:18
  • https://docs.google.com/document/d/1RAKRXfW07kHq0f0j3P9PaU6-fgQXwVUTlklDVaHIDbw/edit – Sergey Russkikh Jan 23 '13 at 10:19
  • Would you please help me to modify it so that it search for variable in message bodies and create new folders corresponding to the variable? – Sergey Russkikh Jan 23 '13 at 10:23
  • see my answer below and try it out – Larry Jan 23 '13 at 10:24

1 Answers1

1

The macro will run for ALL mail in INBOX .. it may takes some time

' run this macro
Sub main_procedure()
    On Error GoTo eh:
    Dim ns As Outlook.NameSpace
    Dim folder As MAPIFolder
    Dim item As Object
    Dim msg As MailItem

    Set ns = Session.Application.GetNamespace("MAPI")
    Set folder = ns.GetDefaultFolder(olFolderInbox)
    MsgBox "Total Number of mail in your inbox " & folder.Items.Count
    For Each item In folder.Items

        If (item.Class = olMail) Then
            Set msg = item
            If InStr(msg.Body, "http://shop.khlynov.net/products/en/") > 0 Then
                URL = msg.Body
                createAndMoveMail URL, msg

            ElseIf InStr(msg.Subject, "http://shop.khlynov.net/products/en/") > 0 Then
                URL = msg.Subject
                createAndMoveMail URL, msg
            End If
        End If
    Next


    Exit Sub
eh:
    MsgBox Err.Description, vbCritical, Err.Number
End Sub



Sub createAndMoveMail(ByVal URL As String, ByRef mail As MailItem)
Dim productID As String
Dim URLPath As String
Dim folderExist As Boolean
Dim startIndex As Long
Dim found As Boolean
On Error goto 0
found = False

Do While Not found
    productID = ""
    startIndex = InStr(URL, "http://shop.khlynov.net/products/en/")
    If startIndex = 0 Then
        Exit Sub
    End If
    URLPath = Mid(URL, startIndex)
    URLPath = Mid(URLPath, Len("http://shop.khlynov.net/products/en/") + 1)
    'update new url
    URL = URLPath
    If InStr(ULRPath, "/") = 0 Then
        Exit Sub
    End If
    productID = Mid(URLPath, 1, InStr(URLPath, "/") - 1)
    If Len(productID) = 19 And InStr(productID, "VOP") > 0 And InStr(productID, "US") > 0 Then
        found = True
        Exit Do
    End If
Loop



If Not found Then
    Exit Sub
End If





Dim myInbox As Outlook.MAPIFolder
Set myInbox = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

folderExist = False
For i = 1 To myInbox.Folders.Count
    If myInbox.Folders.item(i).Name = productID Then
        folderExist = True
        Set myDestinationFolder = myInbox.Folders.item(i)
        Exit For
    End If
Next
If Not folderExist Then
    Set myDestinationFolder = myInbox.Folders.Add(productID, olFolderInbox)
End If

mail.Move myDestinationFolder
End Sub

Reference: read inbox mail item create mail folder,move mail item

Community
  • 1
  • 1
Larry
  • 2,764
  • 2
  • 25
  • 36
  • have a try on this macro, **IMPORTANT** make sure your security setting is `ENABLE ALL MACRO`. If there's any error, let me know WHICH LINE it highlights and what's the error msg – Larry Jan 23 '13 at 10:21
  • Thanks Larry! Where should I place your code? To ThisOutlookSession or to Module 1? Should I see only one macro main_procedure when running macro by pressing Alt+F8 in Outlook? – Sergey Russkikh Jan 23 '13 at 10:40
  • Yes, both locations are fine. Yes, only main_procedure should be seen. Run that macro – Larry Jan 23 '13 at 10:43
  • Would it work with IMAP Inbox folder? How should I change it in order to work? – Sergey Russkikh Jan 23 '13 at 10:55
  • As long as you are using outlook, I think the code will work and if my code works for you, you can mark it as accepted. – Larry Jan 23 '13 at 10:59
  • Just checked. It doesn't work in IMAP folders. Should I change somehting in order to make it work in IMAP? – Sergey Russkikh Jan 23 '13 at 12:33
  • my code is hardcoding olFolderInbox , you may want to take a look (http://www.outlookcode.com/codedetail.aspx?id=628)[loop through outlook folders] Btw, I am away from computer for the next 20hours. – Larry Jan 23 '13 at 13:17
  • Hi, Larry! Not sure if you have read my answer. There are two problems: 1. Macro doesn't work with HTML in message bodies I quoted above. 2. For some reason the macro moves some plain messages and some not so I have to run it again for each message. Would you help me please to fix the code? – Sergey Russkikh Jan 24 '13 at 05:08
  • The problem maybe that the messages have several hyperlinks with different patterns and only last two refer to Product ID – Sergey Russkikh Jan 24 '13 at 05:42
  • Hi, ok wait, so you mean the mail message may have some message with "http://shop.khlynov.net/products/en/" but without product id in the front part of the message? Wait – Larry Jan 24 '13 at 05:50
  • Thanks Larry! I've just checked new code. It doesn't fix the problem. For all plain text messages with only one Product ID link it works fine. But for HTML messages with several different types of hyperlinks (Terms, FAQ, Login, etc.) it moves messages to folder "HYPERLINK: "http" – Sergey Russkikh Jan 24 '13 at 05:53
  • HTML body contains at the beginning links like that: http://www.khlynov.net/fileadmin/mail_templates/ and other subdirs – Sergey Russkikh Jan 24 '13 at 05:58
  • Should I post entire HTML body for your convenience? – Sergey Russkikh Jan 24 '13 at 05:59
  • Thanks, Larry! It now works with HTML! The second problem remains: it stops after moving first two letters. To move others I should rerun macro. What's wrong? – Sergey Russkikh Jan 24 '13 at 06:17
  • @SergeyRusskikh I created 5 consecutive sample mail in your format and the macro would create/move them to the destination folder at once... how many emails are in your outlook inbox? How are the target email distributed in the list inside the outlook inbox? how long does it take to run the macro? Any error message? – Larry Jan 24 '13 at 06:23
  • Hi, try the new code, this should fix a bug to error the script – Larry Jan 24 '13 at 06:34
  • Hi Larry! Sorry, it no longer works even doesn't create folders. I have thousands of emails. First I have to move them manually to Outlook.pst Inbox as macro doesn't work inside IMAP. Previous version of your macro only moved two first messages into appropriate folders inside Inbox and all the rest messages remained inside Inbox root. The macro run about 1-2 seconds. Are there any issue? – Sergey Russkikh Jan 24 '13 at 07:12
  • can you copy the code and try again? Hopefully there should be Some error message. and IT SHOULD NOT run for only 1-2 seconds for 1000+ email. I added a line to show the No. of mail in your inbox, see if the number is correct – Larry Jan 24 '13 at 07:24
  • The new code only shows a message saying: "Total number of email in your Inbox 4056" but doesn't create folders or move messages – Sergey Russkikh Jan 24 '13 at 07:41
  • I tried to debug and it went fine through 2 passes but it stopped at mail.Move myDestinationFolder for the third pass – Sergey Russkikh Jan 24 '13 at 07:51
  • That's strange, 2 things we can do. 1 if it's ok, capture our outlook's structure to imgur and have the link here. 2. Copy my code and raise other question so the experts can help you out. – Larry Jan 24 '13 at 07:52
  • @SergeyRusskikh It stops at that lane, and if you step over, No error message? Please yourself to my latest version. And the current version is WORKING properly for the first 2 mail or NOT? – Larry Jan 24 '13 at 10:03
  • 1
    Do not loop through all messages in a folder. Use Items.Find/FindNext or Items.Restrict. – Dmitry Streblechenko Jan 28 '13 at 18:25
  • @DmitryStreblechenko Thanks for the suggestion. Can you briefly explain to me the reason behind `why not to loop through all message` ? Is it a performance issue? What do you suggest me to use if I need to loop through all MESSAGE-IDs of mails in a folder? – Larry Jan 29 '13 at 07:34
  • 0down vote Yes, it is a performance issue. You are not processing all items in the folder, only those that have a particular string in the message body. – Dmitry Streblechenko Jan 31 '13 at 17:13