I have created a program that searches through my outlook emails for a specific subject line. It works great, however I receive an error whenever one of my strings to search for contains an "&" symbol in it. Here is a sample of the code I'm working with.
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim olFldr As MAPIFolder
Dim olMi As MailItem
Dim olItms As Items
Dim subj As String
Set olApp = GetObject(, "Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = olFldr.Items
subj = "Life&Health Insurance"
Set olMi = olItms.Find("[Subject] = " & subj)
I get a "condition is not valid" error when it runs into anything with an ampersand. Please help.
Try to declare a string variable using the Chr(38) statement:
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim olFldr As MAPIFolder
Dim olMi As MailItem
Dim olItms As Items
Dim subj As String
Set olApp = GetObject(, "Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = olFldr.Items
Set subj = "Life" & Chr(38) & "Health Insurance"
Set olMi = olItms.Find("[Subject] = " & Chr(34) & subj & Chr(34))
Also when filtering text fields, you can use either a pair of single quotes (') or a pair of double quotes (") to delimit the values that are part of the filter. See the Find method of the Items class described in MSDN.