Solved

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

Posted on 2013-12-03
17
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

In this step by step procedure, you will come to know the details of creating an Outlook meeting in 2007, 2010, 2013 & 2016.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

735 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