Solved

Display the resulting search in Outlook 2010 and modify the Outlook displayed result

Posted on 2013-12-03
17
391 Views
Last Modified: 2013-12-25
We are working with a VBA where we want to modify a user-defined column of an item (or email) in a search result displayed.  We have the script that display Outlook windows contents but can't seem to only display the "search result".  

The purpose is access a specific email and change it user-defined column.  But we can't get to display only the result, just the folder contents.
see VBA script
0
Comment
Question by:rayluvs
  • 9
  • 8
17 Comments
 

Author Comment

by:rayluvs
ID: 39692925
forgot scripts:

Script to read in Outlook and get contents:
'GET OUTLOOK CONTENTS
'Set defaults
 Set olFolder = Application.Session.GetDefaultFolder(olFolderInbox)
 Set folderItems = olFolder.Items
    
'Find in Userdefined column PROYECT
 If SearchBy = 1 Then strFilter = "@SQL=" & Chr(34) & "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/Proyecto" & Chr(34) & " like " & "'%" & ProjectSearch & "%'"
 Set FilteredItems = olFolder.Items.Restrict(strFilter)

Open in new window


Script to save to Outlook email user-defined column:
(giving you this script in case needed for what we need)
'SAVE SELECTED CONTENTS
 If OptionSelected Then
    Dim olkMsg As Outlook.MailItem, olkProp As Object
    For Each olkMsg In Application.ActiveExplorer.Selection
        If TypeName(olkProp) = "Nothing" Then
            Set olkProp = olkMsg.UserProperties.Add("Project", olText, True)
            olkProp.Value = ApoyoSelected
            olkMsg.Save
        End If
        Set olkProp = Nothing
    Next
    Set olkMsg = Nothing
    End If

Open in new window

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39694603
Are you expecting the folder to show the restricted set of emails ... because it will not however if at line 9 of your first script you follow up with your second script but at line 4 in that script (as presented) you use the filtereditems set instead of the selection then it should work on only those emails.

Chris
0
 

Author Comment

by:rayluvs
ID: 39700301
Don't quite follow you.

please explain.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39700725
The vba code calculates a vba collection IT DOES NOT modify the outlook displayed folder.

You can however merge your two snippets into one with a small change and it would work
0
 

Author Comment

by:rayluvs
ID: 39705092
Understood.  Just don't quite understand on ID: 39694603, the referencing line 9.  Nevertheless, we want to be specific in what we need.  We want to know if possible to modify the first VBA script in ID: 39692925 so it can search within a search result display in Outlook.  The VBA at this as is, searches the entire inbox folder.  

When we say the "search result" is when user types specific data in the field "Search Inbox (Ctrl+E)", Outlook filters the Inbox to what the user text in (see pic below for the field).

sea1
We want the code to search through the result on screen.  

We want to do this because the Inbox is quite big and the search in the first VBA takes a long a long time.  We figure that if we can have the VBA search within the Outlook Search Result, it would be faster.

Hope this helps describe what we are trying to do.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39705500
First script remains as was:

'GET OUTLOOK CONTENTS
'Set defaults
 Set olFolder = Application.Session.GetDefaultFolder(olFolderInbox)
 Set folderItems = olFolder.Items
    
'Find in Userdefined column PROYECT
 If SearchBy = 1 Then strFilter = "@SQL=" & Chr(34) & "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/Proyecto" & Chr(34) & " like " & "'%" & ProjectSearch & "%'"
 Set FilteredItems = olFolder.Items.Restrict(strFilter)

Open in new window


Second script is called after the first script and during the same process of code execution but is modified at line 4 to become:

'SAVE SELECTED CONTENTS
' Save the set of emails picked out by the VBA filter
 If OptionSelected Then
    Dim olkMsg As Outlook.MailItem, olkProp As Object
    For Each olkMsg In FilteredItems
        If TypeName(olkProp) = "Nothing" Then
            Set olkProp = olkMsg.UserProperties.Add("Project", olText, True)
            olkProp.Value = ApoyoSelected
            olkMsg.Save
        End If
        Set olkProp = Nothing
    Next
    Set olkMsg = Nothing
    End If

Open in new window


Chris
0
 

Author Comment

by:rayluvs
ID: 39707304
Ran it and for some reason it takes the same time.  Placed a counter "X = X + 1" and it counted 713 lines when the Outlook filters screen had 45 lines.  Tried it filtering emails that woul return 4 lines, and it still counted 713.  Note the Inbox folder has 3380 emails.

If I filteres diferente types of emails why always displayed a counted values of 713?
0
 

Author Comment

by:rayluvs
ID: 39707308
How can I know wht is in FilteredItems?
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39707894
Can you supply the combined script you ran please?

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39707896
Note filtered items has the sum of items that match your SQL filter, didn't test the filter itself but if you confirm what it is looking for i'll set up a test for the precise conditions to be sure it works.

Chris
0
 

Author Comment

by:rayluvs
ID: 39733805
Hi sorry for the delay.  The script is the one you recommended to change in ID: 39705500 where we change to "FilteredItems".

Yes, that is what we are looking for, we are looping thru "FilteredItems".
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 39733903
You are using a variable projectsearch in your code which if blank will simply return the wild cards.  This is why I asked about the script you are using ... What should the search term look like?

Chris
0
 

Author Comment

by:rayluvs
ID: 39733906
understood, thanx!  Let me try something?
0
 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 500 total points
ID: 39733938
Of course but I just ran a test of the sample code:

Sub test_28309084()
'############### SCRIPT 1
'GET OUTLOOK CONTENTS
'Set defaults

Dim olkMsg As Object
Dim olkProp As Object
Dim olkMessage As Object
Dim olkFolder As Object
Dim folderItems As Object
Dim strFilter As String
Const projectSearch As String = "ProjectSearch"
Const searchBy As Integer = 1
    
    Set olkFolder = Application.Session.GetDefaultFolder(olFolderInbox)
    Set folderItems = olkFolder.Items
    
'Find in Userdefined column PROYECT
    If searchBy = 1 Then strFilter = "@SQL=" & Chr(34) & "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/Proyecto" & Chr(34) & " like " & "'%" & projectSearch & "%'"
    Set FilteredItems = olkFolder.Items.Restrict(strFilter)
 
'############### SCRIPT 2
'SAVE SELECTED CONTENTS
' Save the set of emails picked out by the VBA filter
    If OptionSelected Then
        For Each olkMsg In FilteredItems
            If TypeName(olkProp) = "Nothing" Then
                Set olkProp = olkMsg.UserProperties.Add("Project", olText, True)
                olkProp.Value = ApoyoSelected
                olkMsg.Save
            End If
            Set olkProp = Nothing
        Next
        Set olkMsg = Nothing
    End If
End Sub

Open in new window


With the constant projectSearch undefined, (i.e. without line 12 initially) it returns all items whereas with it set to "projectsearch" (the variable now defined as the string "ProjectSearch") then since I do not have the user defined column it returns 0 items confirming to me what I was saying about the wild card search.

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39734018
Note

I will admit to assuming that your returned count of 713 refers to mails with ANY value for the user defined field whereas you are looking for values that match your specific string value.

Chris
0
 

Author Comment

by:rayluvs
ID: 39739478
sorry for the delay but thank you a million!  The problem was the variables which definitely overlooked completely.
0
 

Author Closing Comment

by:rayluvs
ID: 39739679
Thanx!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question