Outlook VBA

Peterho69

Senior Member
Joined
Apr 5, 2004
Messages
1,183
Reaction score
5
Hi...Any1 here who is good in outlook VBA.

I would like to a have a VBA which will find a string of text in the email and then put it into a folder.

I hope someone can give me some tips on how to do it.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Hi...Any1 here who is good in outlook VBA.

I would like to a have a VBA which will find a string of text in the email and then put it into a folder.

I hope someone can give me some tips on how to do it.

Do you need VBA for such stuff ? Just create a mail rule that search for matching criteria in the mails and assign the action to move them to a folder.
 

Peterho69

Senior Member
Joined
Apr 5, 2004
Messages
1,183
Reaction score
5
Do you need VBA for such stuff ? Just create a mail rule that search for matching criteria in the mails and assign the action to move them to a folder.

As I need to select a partial string. Rules do not accept string in wildcard.

Eg.

023B extract 23 to folder 23
023A extract 23 to folder 23
123J extract 23 to folder 23
025A extract 25 to folder 25
225H extract 25 to folder 25

I only need the 2nd + 3rd string of text to move it to a folder. This information is store in the body of the message.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
As I need to select a partial string. Rules do not accept string in wildcard.

Eg.

023B extract 23 to folder 23
023A extract 23 to folder 23
123J extract 23 to folder 23
025A extract 25 to folder 25
225H extract 25 to folder 25

I only need the 2nd + 3rd string of text to move it to a folder. This information is store in the body of the message.

I see, conditional rule. Okay, I found the base idea for you at VBA code to set rules to move messages with specific words in subject to a folder (for Outlook 2007 and Outlook 2010)

However you may need to use regular expression if the search is too convoluted. Here is some article on it in VBA Returning a regex match in VBA (excel) - Stack Overflow.

The idea is suppose the content of your message has such exact phrase "ABCDEF 225H XYZW" your regex should be "ABCDEF\s+\d(\d\d)\w\s+XYZ", so that you can match in a more precise manner and still be flexible enough to get the 2 digits out from the capture group, then create a folder on the fly, and move the mail to the folder.
 

Peterho69

Senior Member
Joined
Apr 5, 2004
Messages
1,183
Reaction score
5
I have found this code which will search for the sender name and then put it into a folder. How do I make the changes and do a search in the body of the messages:-

Sub MoveItems1()

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim myItem As Object


Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myInbox.Folders("aaaa").Folders("intake1")
Set myItem = myItems.Find("[SenderName] = 'ABC COY'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend

End Sub
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
I have found this code which will search for the sender name and then put it into a folder. How do I make the changes and do a search in the body of the messages:-
Code:
Sub MoveItems1()
    
    Dim myOlApp As New Outlook.Application
    Dim myNameSpace As Outlook.NameSpace
    Dim myInbox As Outlook.MAPIFolder
    Dim myDestFolder As Outlook.MAPIFolder
    Dim myItems As Outlook.Items
    Dim myItem As Object


    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
    Set myItems = myInbox.Items
    Set myDestFolder = myInbox.Folders("aaaa").Folders("intake1")
    Set myItem = myItems.Find("[SenderName] = 'ABC COY'")
       While TypeName(myItem) <> "Nothing"
        myItem.Move myDestFolder
        Set myItem = myItems.FindNext
    Wend

End Sub

Based on the Outlook VBA reference found at Items.Find Method (Outlook)

You might want to NOT use the Filter approach because from the reference it don't allow searching thru the body of the mail items, you need to iterate one by one and perform your matching. Your matching is not just looking for keyword that simple

Try replacing the following
Code:
Set myItem = myItems.Find("[SenderName] = 'ABC COY'")

with

Code:
Dim myItem As MailItem
Set myItem = myItems.GetFirst
While TypeName(myItem) <> "Nothing"
  'You Can Access the mail's body using myItem.Body here
  Set myItem = myItems.GetNext
Wend

I don't do VBA much, so you might want to work your way out any typing issue
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top