Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

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

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
rayluvs
Asked:
rayluvs
  • 9
  • 8
2 Solutions
 
rayluvsAuthor Commented:
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
 
Chris BottomleyCommented:
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
 
rayluvsAuthor Commented:
Don't quite follow you.

please explain.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
Chris BottomleyCommented:
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
 
rayluvsAuthor Commented:
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
 
Chris BottomleyCommented:
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
 
rayluvsAuthor Commented:
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
 
rayluvsAuthor Commented:
How can I know wht is in FilteredItems?
0
 
Chris BottomleyCommented:
Can you supply the combined script you ran please?

Chris
0
 
Chris BottomleyCommented:
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
 
rayluvsAuthor Commented:
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
 
Chris BottomleyCommented:
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
 
rayluvsAuthor Commented:
understood, thanx!  Let me try something?
0
 
Chris BottomleyCommented:
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
 
Chris BottomleyCommented:
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
 
rayluvsAuthor Commented:
sorry for the delay but thank you a million!  The problem was the variables which definitely overlooked completely.
0
 
rayluvsAuthor Commented:
Thanx!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now