Link to home
Start Free TrialLog in
Avatar of Takamine334
Takamine334Flag for United States of America

asked on

Help with Macro to customize search

Does anyone know of a macro that I can use that will customize a search on the SENT ITEMS folder? I need it to read from the clipboard. So, it'd be something like this:

1. CTRL-C an email address in the text of the email message body
2. Hit a HOTKEY invoking the Macro
3. The macro will search the SENT ITEMS folder with the clipboard.settext function
4. The macro will return a value showing if the email on the clipboard is located in the SENT ITEMS folder.
Avatar of David Lee
David Lee
Flag of United States of America image

Hi, Takamine334.

A couple of questions.
  1. Do you just want to know if the folder contains a match, or do you want to see the matches like you would if you'd used advanced search?
  2. Should the search only look in address fields (i.e. TO, CC, BCC) or does it need to look in message text too?
Avatar of Takamine334

ASKER

1. I would want to see the match if it occurs
2. I would like to see if the match occurs in the TO text box.
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First off, I think you should be given the Mensa Award.

Now that that's said, I highlighted and copied an email address in a message. I ran the macro and it pulled up:

Results of Search Search1
There were 1 matches.
NACA Question

When I clicked on the link, nothing happened. Can you tell me if I'm doing something wrong?


Actually, another IE window appeared with the URL:
outlook:00000000D2BB204E2AD386469DE63453C260EE8EA4882300


The page said:
The webpage cannot be displayed
   
   Most likely cause:
Some content or files on this webpage require a program that you don't have installed.
 
   What you can try:
     Search online for a program you can use to view this web content.  
 
     Retype the address.  
 
     Go back to the previous page.
 
Also, I'm getting the below response when using this macro. Is there anyway to just open up the SENT ITEMS folder that stores this email?
Clipboard01.gif
Thanks for the thought.

You aren't doing anything wrong.  By default Office/Outlook 2007 does not install a URL protocol handler for links to Outlook items.  The protocol handler is just a couple of registry entries.  There's an explanation and .reg file download tht'll add the entries for you at this page: http://www.davidtan.org/outlook-2007-adding-outlook-url-protocol/  After applying the .reg file you may have to edit the entries.  Go ahead and install the .reg file, then try a one of the links and let me know what happens.
Absolutely perfect. Would it be possible to put this command in a (right click) menu link?

1. Select hyperlink
2. ALT+F8
3. Select THISOUTLOOKSESSION.MYSEARCH
Once again, BlueDevil rocked it!
The dialog-box you took the screenshot of is generated when reading data from the clipboard.  Try replacing MySearch with the version below.  You'll need to add a reference to FM20.DLL in Outlook.  To do that
  1. Open the VB editor in Outlook
  2. Click Tools > References
  3. Click the Browse button
  4. Go to the Windows System32 folder
  5. Find and add FM20.DLL
Sub MySearch()
    Const strScope As String = "'Sent Items'"
    Const strTag As String = "Search1"
    Dim objData As New DataObject, _
        olkSearch As Outlook.Search, _
        strFilter As String, _
        strBuffer As String
    objData.GetFromClipboard
    strBuffer = objData.GetText()
    strFilter = "urn:schemas:httpmail:displayto LIKE '%" & varText & "%'"
    Set objSch = Application.AdvancedSearch(Scope:=strScope, Filter:=strFilter, SearchSubFolders:=False, Tag:=strTag)
End Sub

Open in new window

Did the above and the error is gone, however, when searching an email I get:

Results of Search Search1
There were 0 matches.

I'm actually looking at the SENT ITEMS folder that does contain that email.
Oops.  Typo.  Disregard that last version and use this one instead.

Sub MySearch()
    Const strScope As String = "'Sent Items'"
    Const strTag As String = "Search1"
    Dim objData As New DataObject, _
        olkSearch As Outlook.Search, _
        strFilter As String, _
        strBuffer As String
    objData.GetFromClipboard
    strBuffer = objData.GetText()
    strFilter = "urn:schemas:httpmail:displayto LIKE '%" & strBuffer & "%'"
    Set objSch = Application.AdvancedSearch(Scope:=strScope, Filter:=strFilter, SearchSubFolders:=False, Tag:=strTag)
End Sub

Open in new window

I'll work on the context menu angle.
I see that varText  = Empty
Changed varText to strBuffer and it works.
Would it be possible to put this command in a (right click) menu link?

1. Select hyperlink
2. ALT+F8
3. Select THISOUTLOOKSESSION.MYSEARCH
The right-click, "context", menus in Outlook are separate entities.  Do you want this added to all of them or only one?  If one, which one?
I'm sorry, I don't understand the question.
No problem.  Te right-click menus in Outlook are called "context" menus.  The selections offered on a menu are in the "context" of the click.  For example, right-clicking on a folder displays selections that apply to a folder context.  Right-clicking on a message displays selections in the context of a message.  Outlook defines the following six contexts: Attachment, Folder, Item, ShortCut Store, and View.  My question is whether you want a menu selection added to all of them, some of them, or only one of them?  In other words, do you want the search option to appear when you right-click on an attachment, a folder, a vew, etc?  Unfortunately the context menu that appears when you right-click on an address isn't programmable.
I see. Thank you for that information. I would assume I'd only need that function when I right click an email when viewing emails in the preview pane or when they are double clicked and the email message is in its own form.
Sorry to be slow getting back to you.  None of the context menus work in open items.  Are you sure you wouldn't prefer a toolbar button?
A button would work fine. Thank you.
Apologies again for being so slow.  Here's how to add a toolbar and button for running a given macro.

1.  Click View->Toolbars->Customize
2.  Click the Toolbars tab
3.  Click New
4.  Name the toolbar
5.  Click the Commands tab
6.  Under Categories click Macros
7.  Under Commands click and hold on the macro, then drag it out and drop it on the new toolbar
8.  Dock the toolbar somewhere onscreen