Macro for Instant Search in new Window, failes Instant search (OL2

E

Eylon

Hello Everyone,

I wrote a Macro that takes the selected Email, removes some text from the
subject (e.g. “RE:, FW:†etc.) and performs a search on all Emails containing
this string. The results of the search are displayed in a new window.
Sometimes it works as expected, and sometimes I get an error:
This is how the error object holds these values:
Description: "The operation failed."
HelpContext: 1000440
Number: -2147219964
From when it fails, Instant search stops responding in the main window as
well. All this until I restart Outlook (2007).
Interestingly, when I change the Macro to display the results in the current
(main) window, it always work without any problem.
The code:
******************************
Sub SearchByStrippedSubject()
Dim app As New Outlook.Application
Dim item As Object
Dim email As MailItem

Set email = app.ActiveExplorer.Selection.item(1)
Dim myExplorer As Explorer

On Error Resume Next
Set myExplorer =
app.Explorers.Add(app.Session.GetDefaultFolder(olFolderInbox),
olFolderDisplayNormal)

Dim filter As String

filter = email.Subject
filter = Replace(filter, "RE: ", "")
filter = Replace(filter, "FW: ", "")
filter = Replace(filter, "RE:", "")
filter = Replace(filter, "FW:", "")
filter = Replace(filter, " (handled)", "")
filter = Replace(filter, "(handled)", "")
filter = Replace(filter, " (handling)", "")
filter = Replace(filter, "(handling)", "")

filter = Chr(34) + filter + Chr(34)

On Error Resume Next
myExplorer.Search filter, olSearchScopeAllFolders
If Err.Number <> 0 Then
MsgBox Err.Description
Err.Clear
myExplorer.Close
Exit Sub
End If
myExplorer.Display

End Sub
******************************

Any ideas?
TIA,
 
K

Ken Slovak - [MVP - Outlook]

This runs in the Outlook VBA project? If so don't use New
Outlook.Application, use either Application or set app = Application. See if
that helps.

The error is a generic MAPI_E_COLLISION error, and that usually occurs if
you try to create a folder with the same name as another folder in the same
hierarchy location. So it's possible that displaying a new Explorer is
what's doing that and you might have to fall back on just using the existing
Explorer.
 
E

Eylon

Thanks Ken,
I run it from the Tools menu -> Macro -> Macros. Guess it makes it a VBA
project.
Tried your solution but it behaves the same (first time works, second
results in error, then must restart for normal GUI Instant search to work in
main window).

It does behave as expected if I run it from the GUI by right-clicking
Personal Folders in the Navigation Pane, Selectin Open in New Window, and
entering the search in the Instant Search box. With the macro I try to mimic
the same action.

Any other ideas?
Thanks again,
 
K

Ken Slovak - [MVP - Outlook]

Only other idea would be to display the new Explorer before running Search
on it. Otherwise, no ideas other than using the original Explorer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top