Use of AdvancedSearch with wildcards in Outlook

G

Guest

I have been trying to build a search routine that will open the inbox and
cycle through the body of each message looking for a string that I define
using wild cards and the Like operator.

Here is the code snippet:

------------------------
Do While n < filecount
Set myitem1 = myFolder.Items(n) 'iterate through items in inbox

' send (myitem1) to a string search
Dim objSch As Search 'name of the search obj result
Const strF As String = "myItem1.body" Like "<*@*>" 'the actual search
string
Const strS As String = "myItem1" 'where to look for the search string
Const strTag As String = "AddressSearch" 'search name tag

'On Error Resume Next
Set objSch = Application.AdvancedSearch(Scope:=strS, Filter:=strF,
Tag:=strTag)

--------------

Can't get this advancedSearch to run; get automation errors. suspect that
the search string is tripping up the search call.

Can you suggest anything? Is is OK to use the Like operator in
advancedSearch?
 
G

Guest

Well, I'm not an mvp with vba for outlook2003, or any vb, for that matter.

Can you tell from the code provided if I'm doing something fundamentally
wrong to achieve an automation error?

If you look at the code below, you will see that the code I am currently
working is a detailed part of the broader code project. thanks for the help
with the MAPI call. I haven't had to do those before. The whole notion in
outlook of having to call a mapi store, namespace, etc, while understandable
in mail terminology, is foreign to access, word, excel macro programming.

this code is being written for outlookk 2003. My actual current coding
language is vs2005. that also causes some confusion, as notations I expect to
work in one doesn't work in the other.

Hope that clarifies things for you. Yes, thank you for your previous help
understanding the vaguities of MSO2003VBA. Coding in it has been a tedious
expereince!
 
G

Guest

Another attempt to be clearer: I can't get an advancedSearch to search for a
string containing wild card characters. My only reference is VS2003, which
says 'like' operator admits use of wild cards. Syntax for the string is
unclear, but appears that asterisk can be used inside the double quotes as a
wild card character; that no connectors (for ex: & or + ) are required
between character substrings; that no escape characters are used to indicate
how asterisk is read within the string.

I can't get the advancedSearch method to work even though the rest of the
code works fine, as evidenced by the creation of the text file, the correct
count of messages in the selected outlook folder, and demonstrated ability to
write the body text to file.

Everything works except the advancedSearch routine, and it is a pparently
the only way to use wild card characters in a string search and return char
results, not positional or boolean results.
 
M

Michael Bauer [MVP - Outlook]

Please go back to the original thread and read my two last messages. The one
gives you a complete sample for using the AdvancedSearch method, it also
shows how to use the like operator. The other shows very detailed how to
loop through the folder yourself and search for strings.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Quick-Cats - Categorize Outlook data:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>


Am Tue, 8 May 2007 08:32:02 -0700 schrieb Ed:
 
G

Guest

None of the examples I have seen, either here or in help or texts, show clear
use of wild cards in the advanced search. Your examples are much like the
ones in help in vba for outlook 2003. They search for specific strings.

Do you know if strings containing wild cards need to be concatenated such as
"," & * & "@" etc.

AdvancedSearch is tripping offline either because it doesn't like the call
to the body text, which I think is incorrect, or because it doesn't like the
construction of the search string in SrtF.

Using the DASL string that includes 'httpmail' yields results that are
damaging to the efficiency of the search, as they bring up all of the html
tags, which are built like the search string.

In your code, you used % as a wild card, which my outlook compiler doesn't
like.
 
G

Guest

If I use exactly what you sent, the editor complains about use of % in the
advancedsearch StrF string. All other components up to that point seem to
work fine. Really appreciate the select folder dialog box code, and it works
great.
 
G

Guest

The code is below for the advancedSearch call. The percent sounds C# to me. I
would send 'original' but am coding along here at cowboy speed, and am
developing it on a box that doesn't have internet connectivity. The problem
seems to be in the use of wild cards in a DASL search in the VBA
advancedSearch routine.
 
G

Guest

Sub SearchUndelsFolder()

'Dim myFolder As Results
Dim filecount As Long

'establish two variables
Dim n As Integer
n = 1
Dim address As String
'added code
Dim myOlApp As Object
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim myBody As Variant
Dim lngMsgLen As Long
Dim myitem1 As Object


'create output file system object
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\FailedAddresses.txt", True)

'call the Outlook application MAPI interface
Set myNameSpace = Application.GetNamespace("MAPI")

'Address the Inbox folder
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)

'count entries in Inbox folder
filecount = myFolder.Items.Count
MsgBox ("There are " & filecount & "messages in the list")

'For Each Item In myFolder

'for each message body in the MAPI undeliverables subdirectiry
'address the message body of the nth entry in MyFolder

Do While n < filecount
Set myitem1 = myFolder.Items(n) 'iterate through items in inbox

'next line works; writes entire body to text file
'a.writeline (myitem1.Body)

'instead, send (myitem1) to a string search
Dim objSch As Search 'name of the search obj result
Const strF As String = "urn:schemas:httpmail:textdescription LIKE
'<*@*>'" 'the actual search string
Const strS As String = "myItem1" 'where to look for the search string
'added dot body to above line
Const strTag As String = "AddressSearch" 'search name tag

'run the search
'On Error Resume Next
Set objSch = Application.AdvancedSearch(Scope:=strS, Filter:=strF,
Tag:=strTag)

'open an obj inspector on objSch and write results of objSch to file
Set myOlApp = CreateObject(objSch)
Set myItem = myOlApp.ActiveInspector
Set objItem = myitem1.CurrentItem 'Current eMail MUST BE OPEN!
'myBody = objItem.Body
' address = myItem.Find Like "<*@*>"
' a.writeline (address)
'reset n and address values
a.writeline (objItem)
objItem = ""
n = n + 1
Loop

End Sub
 
G

Guest

Sorry, but I don't follow you, and am tired of the guessing game. You have
been a waste of time, as well as rude and insulting.
 
M

Michael Bauer [MVP - Outlook]

Sorry to hear that. To save your and my time, *please* stop guessing, but
simply take our tipps; use at last the sample given by me on the 3/5. That's
the way AdvancedSearch works - we can't change that.

Your 'guessing game' is that you're still looking for something to replace
the % by anythign else. Instead, simply replace the value in strS by the
standard folder's name - and additionally make use of the
AdvancedSearchComplete event. That's all.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Quick-Cats - Categorize Outlook data:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Fri, 11 May 2007 09:58:01 -0700 schrieb Ed:
 
S

Sue Mosher [MVP-Outlook]

To underline one of Michael's points, % is the wildcard character in an AdvancedSearch search and should be either the first or last character (or both) of a search string. (There is no support for internal wildcards.) You can't change that just because you think it looks C#-ish.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

Sue, thank you for that. I'm not trying to change it to C#, but trying to get
it to run. Michael apparently doesn't understand that I am not an expert
with VBA for Outlook 2003, and am struggling to understand why this doesn't
work.

Michael refers to code he sent on 3/5, but I cannot find it in the confused
tangle of messages in the VBA community board.
 

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