Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Text Within The Subject With Outlook Macro

Status
Not open for further replies.

Dawber

Technical User
Jun 29, 2001
86
GB
I am attempting to single out Outlook messages with a particular word in the subject, using an If / Then statement. I am struggling with the syntax, but you ought to be able to work out what I am after from this:

If mItem.Subject INCLUDES “xxx” Then
Blah Blah Blah
End If


Obviously, it is the INCLUDES bit that I do not understand how to accomplish, or even if it is possible. Any assistance would be gratefully received.
 
Have a look at the Like operator.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for the info chaps. Unfortunately, I am still struggling to achieve my goal, which is:

1. Check each message in the Inbox
2. Date stamp (YYYYMMDDHHMM Format) each message subject that has not previously been "stamped"

So far, I have got:

Code:
Sub AddDateToSubject()

Dim mItem As Object
Dim oFolder As Object
Dim j As Long

Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)

For j = myFolder.Items.Count To 1 Step -1

If mItem.Subject Like Format(mItem.SentOn, "YYYYMMDDhhmm*") Then
j = j - 1
End If

Set mItem = myFolder.Items.Item(j)

mItem.Subject = Format(mItem.SentOn, "YYYYMMDDhhmm") & " " & mItem.Subject
mItem.Save
Next j
    
Set mItem = Nothing
Set myFolder = Nothing
    
End Sub

This macro falls over at the Like operator.

The alternative solution would be to run a macro whenever a new message is received, though again, I do not know how to achieve this.

Assistance with either / both solutions would be welcomed.
 
If I copy your code, it falls over WAY before the Like.

1. You appear to not being using Option Explicit in your module. I highly recommend you do this. It requires you to declare your variables. Most people do use Option Explicit. I do. It stops the procedure on the very first instruction:
Code:
Set myOlApp = CreateObject("Outlook.Application")
because myOlApp is not declared. VBA has no idea what it is, and refuses to do anything with the instruction. One reason it is good to declare variables, and use Option Explicit is that you do not get situations like:

You do NOT declare the Outlook objects, but you SET them.

You DO declare mItem as an Object...and you DO destroy it with = Nothing...but...you never SET it.

You can declare mItem as an Object, but if you never SET it to actually be something, well...it can hardly be Like anything.

2. Did you step through the code? I would recommend you do so when you have problems.

Gerry
My paintings and sculpture
 
Yes, everything Gerry says, except that ...

... you do set mItem. It's just that you don't set it until after trying to use it.

When you correct that you will find you run into other problems - like the asterisk in the Format.

But you don't really need the Like operator to check the beginning of the string - you can use Left.

Also it's not good practice, IMO, to change control variables inside the loop which they are controlling - and what you have tried won't work anyway because you carry on after decrementing it.

What you want (in pseudocode) is

For Each Item
If Left(Item.Subject, n) <> Item.SentOn Then
Item.Subject = Item.SentOn & Item.Subject
EndIf
EndIf


Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top