Outlook 2003... search for line break char

  • Thread starter Thread starter Ben Bradley
  • Start date Start date
B

Ben Bradley

Hi everyone

I've got a folder full of website e-mail forms, so all the messages are
of the same format.
We're using Outlook 2003 in an Exchange environment.

I need to search through and find a particular set of messages that have
the following string:

double-sized listing?
Yes

Searching for just the string "double-sized listing?" won't work as all
the forms have that text.

The problem is I can't find a way to search for the line-break character.

In MS Word you can search for special characters... I would accomplish
the above search with the following...
double-sized listing?^NYes

But in Outlook that doesn't appear to work.

Is there a way to search for this in Outlook?
I assume there is a way, but I guess it might only be possible with VBA?


Next time I think we are going to have to re-design our e-mail form to
give the question and answer all on the one line.

Thanks
Ben
 
Yes, you'll need code to do that. If you're searching the Body property, look for vbCrLf.
 
Hi

Is there a complete example of the VBA script I need to complete this?
I'm completely new to VBA but that would give me a great starting point
to try some other searches of that nature.


Also, I guess I need to load the Outlook VBa addin off the CD?
We're using Outlook 2003.


Thanks
Ben
 
VBA installs as part of the default setup. If you're completely new to it, start with the basics here: http://outlookcode.com/article.aspx?id=49

Your macro (not script) would need to return the Inbox with GetDefaultFolder, then iterate over its Items collection, something like this:

Sub DoSearch()
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim strFind As String
On Error Resume Next
Set fld = Application.Session.GetDefaultFolder(olFolderInbox)
strFind = "double-sized listing?" & vbCrLf & "Yes"
' or vbLf it's line feed not a complete CRLF
For Each itm In fld.Items
If InStr(1, Item.Body, strFind, vbTextCompare) > 0 Then
MsgBox "You found one!"
End If
Next
Set itm = Nothing
Set fld = Nothing
End Sub

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
Hi Sue
Perfect. I got that code to work and the MsgBox repeatedly popped up on
the screen.

All the website form e-mails are in a particular sub-folder of my inbox.
So that worked.

Is there any way to generate an Outlook find results list of these messages?


Thanks so much for your help so far!
Ben
 
Is there any way to generate an Outlook find results list of these messages?

You mean display them in the dialog for Advanced Find? Not programmatically. You'd need to provide your own display UI. Alternatively, use code to assign a category to those items that match your criteria, then search manually on that category.
 
Ah ok.
That will still work I guess.

So in the VBA code, how do I flag a message a certain colour?

In this section of code:
For Each itm In fld.Items
If InStr(1, Item.Body, strFind, vbTextCompare) > 0 Then
MsgBox "You found one!"
End If
Next


I need to change the...
MsgBox "You found one!"
line to set a flag colour for that message.

What's the VBA for that?


Also, is there a complete online reference of all VBA functions / commands?


Thanks
Ben
 
Take a look at the MailItem.FlagIcon property. The complete reference is in object browser -- F2 in VBA -- as well as on MSDN.
 
Hi Sue
Thanks for all your help so far.
If all the e-mails are in a certain folder, how do I tell the script to
process just a particular sub-folder.

We're in an Exchange environment and the folder is located:
Inbox\BB Website Forms


This is what I've got so far:
Sub DoSearch()
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim strFind As String
On Error Resume Next
Set fld = Application.Session.GetDefaultFolder(olFolderInbox)
strFind = "double-sized listing?" & vbCrLf & "Yes"
' or vbLf it's line feed not a complete CRLF
For Each itm In fld.Items
If InStr(1, Item.Body, strFind, vbTextCompare) > 0 Then
'MsgBox "You found one!"
itm.FlagIcon = olYellowFlagIcon
itm.Save
End If
Next
Set itm = Nothing
Set fld = Nothing
End Sub



Thanks
Ben
 
Every folder has a Folders collection that represents its subfolders. Each subfolder in a Folders collection must have a unique name. Hence, you can use fld.Folders("BB Website Forms").
 
Hi Sue

This is what I've changed the code to, but it doesn't seem to work.

I was hoping it would flag each email with a yellow flag, but when I
click 'Run' it just disappears and no mails are flagged.


Sub DoSearch()
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim strFind As String
On Error Resume Next
Set fld = Application.Session.Folders("BB WEBSITE FORMS")
strFind = "double-sized listing?" & vbCrLf & "Yes"
' or vbLf it's line feed not a complete CRLF
For Each itm In fld.Items
If InStr(1, Item.Body, strFind, vbTextCompare) > 0 Then
itm.FlagIcon = olYellowFlagIcon
itm.Save
End If
Next
Set itm = Nothing
Set fld = Nothing
End Sub



Do I need to specify that it's actually a sub-folder within my Inbox?
Or does the macro just look at the folder name and know where in the
Mailbox it's located?
What happens if you have many folders with the same name but in
different locations within your mailbox?


Thanks
Ben
 
Do I need to specify that it's actually a sub-folder within my Inbox?

Absolutely, using the Folders collection of the Inbox folder, not the Namespace object.

You can't just set an icon. You must also actually flag the messages. That involves setting the FlagStatus property to olFlagMarked.

If you step through the code, is it finding the text in the body? If not, maybe the break is a vbLf, not a vbCrLf.
 
Hi Sue

When I initially tried your version of the script, the msg box saying "I
found one!" popped up several times.

So line 8 is obviously returning true at some point.


So this is what I've modified the code to:

01 Sub DoSearch()
02 Dim fld As Outlook.MAPIFolder
03 Dim itm As Object
04 Dim strFind As String
05 On Error Resume Next
06 Set fld = Application.Session.GetDefaultFolder(olFolderInbox)
07 'Set fld = Application.Session.Folders("BUILDER BOOK TEST")
08 strFind = "double-sized listing?" & vbCrLf & "Yes"
09 ' or vbLf it's line feed not a complete CRLF
10 For Each itm In fld.Items
11 If InStr(1, Item.Body, strFind, vbTextCompare) > 0 Then
12 MsgBox "You found one!"
13 itm.FlagStatus = olFlagMarked
14 itm.FlagIcon = olYellowFlagIcon
15 itm.Save
16 End If
17 Next
18 Set itm = Nothing
19 Set fld = Nothing
20 End Sub


When I run the above macro all it seems to do is flag all the messages
in my inbox to a yellow flag.

Note lines 6/7 where I'm trying to specify the folder.
Line 6 was your initial example, which seems to flag all messages in the
inbox as yellow.
Line 7 is my attempt which doesn't do anything... running the macro the
screen flickers a bit then just returns to the macro screen.

Is my line 7 syntax correct?


Thanks
Ben
 
No, line 7 is not correct. As I said earlier, every folder has a Folders collection, and you need to work with the Folders collection of the Inbox to return your folder:

Set fld = Application.Session.GetDefaultFolder(olFolderInbox)
Set fld = fld.Folders("BUILDER BOOK TEST")
 
Hi Sue

Ah ok, that's got me back on the right track.

Just tried it and it just flagged every single message in that BUILDER
BOOK TEST folder with a yellow flag.

Then I tried changing it from vbCrLf to vbLf and the same happened... it
flagged all the messages yellow.

So the only remaining problem is to get the macro to match the messages
correctly.

Any ideas?


Thanks
Ben
 
You're the only one who can walk through the code to see where the matches are occurring -- literally, at what position Instr() says the match appears in the text -- and then examine the text to see what's going on.
 
Back
Top