First off, looping through an array retrieved from a Split function is not an
intensive operation at all.
The main issue with your solution is that you have a dynamic number of
custom fields for document names. Since you can't do a full text search on a
given field using the Restrict of Find methods (e.g. look for occurrence of
"xyz"; only pattern matching is supported - e.g. look for "xyz****" or
"****xyz"), you have to loop through all items in the folder and inspect the
value of each desired field using the InStr function:
Sub SearchCommonFieldsExample()
Dim objItems As Outlook.Items, objFolder As Outlook.MAPIFolder
Dim objItem As MailItem
Dim objUserProperties As Outlook.UserProperties, objUserProperty As
Outlook.UserProperty
Dim intX As Integer
Dim intTextFoundLocation As Integer
Dim strBaseFieldName As String
strBaseFieldName = "Doc"
'set objFolder = Whatever folder that contains item you need to loop
through
Set objItems = objFolder.Items
For Each objItem In objItems
For intX = 1 To 3
Set objUserProperties = objItem.UserProperties
Set objUserProperty = objUserProperties(strBaseFieldName & intX)
'Look for Doc1, Doc2, or Doc3 field
If Not objUserProperty Is Nothing Then
intTextFoundLocation = InStr(objUserProperty.Value,
"TextToSearchFor")
If intTextFoundLocation <> 0 Then
'VALUE FOUND!
End If
End If
Next
Next
End Sub
If the field names that you need to search share common text, then all I'm
saying is you can cut down on your code by looping through the variations on
that name. However, this is another approach to looping through all items in
a folder, by using the AdvancedSearch method and looping through the common
fields as before:
Sub AdvancedSearchExample()
Dim objSearch As Outlook.Search
Dim strBaseFieldName As String
Dim strDASL As String, strSearch As String
Dim intX As Integer
strBaseFieldName = "Doc"
For intX = 1 To 3
strDASL =
"
http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/"
strSearch = strDASL & strBaseFieldName & intX = " LIKE '%Text To
Search For%'" 'Do a full text search using LIKE predicate
Set objSearch = Application.AdvancedSearch("Inbox", strSearch)
Next
End Sub
Note that I still have to dynamically build the search string to iterate
through the variations of the field names. If the names had no common
string, then you'd have to repeat several lines of nearly identical code,
which isn't very efficient.
There's more to AdvancedSearch than the example I'm showing though; look at
the example in the Outlook VBA help file ("C:\Program Files\Microsoft
Office\OFFICE11\1033\VBAOL11.CHM" for OL 2003, or press F1 while your cursor
is within a property or method in the VBA Editor).
If your custom fields are being overwritten, then it sounds like you have
mapped multiple controls to the same field! Ensure that each control is
mapped to a unique field name (Doc1, Doc2, etc.).
--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
Try Picture Attachments Wizard for Outlook!
http://tinyurl.com/9bby8
Job:
http://www.imaginets.com
Blog:
http://blogs.officezealot.com/legault/