automating body text search

E

Edward

Hello,
I have an Outlook folder full of undeliverable emails. I am trying to
develop a macro that will go to the folder, open each email, extract the
reported failed address frm the body of the email, and save the address to a
file. The failed address regularly appears in angle brackets in the body of
the message.

I can't seem to identify the best approach to this. Can you tell me what
I'm doing wrong, or if there is a more straitforward approach. I tried an
advanced search, but it stalls on the command line. I need to get this to
work so I can install the loop, and add a file location in which to save the
addresses located in the body of the messages. Currently it's a form with
one button:

Private Sub CommandButton1_Click()

Dim objSch As Search
Dim strF As String
Dim strS As String
Dim strTag As String

strS = "undeliverables"
strF = "urn:schemas:httpmail:body LIKE '<' & * & '>'"
strTag = "EmailSearch"
' TODO: Replace Application with the declared and set
' Outlook Application object
objSch = Application.AdvancedSearch(strS, strF, False, strTag)


End Sub
 
E

Edward

Actually, I am trying this code:

Private Sub FindSaveAddressInEmails()
On Error Resume Next
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objItem As Object
Dim strFind As String
Dim objItems As Outlook.Items

Dim objSch As Search
Dim strF As String
Dim strS As String
Dim strTag As String

Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.PickFolder
If objFolder Is Nothing Then Exit Sub

Set objItems = objFolder.Items

For Each objItem In objItems

strS = "undeliverables"
strF = "urn:schemas:httpmail:body LIKE '<' & * & '>'"
strTag = "EmailSearch"
objSch = Application.AdvancedSearch(strS, strF, False, strTag)

' objItem.Save
Next

MsgBox "Finished.", vbOKOnly + vbInformation

Set objNS = Nothing
Set objFolder = Nothing
Set objItem = Nothing
Set objItems = Nothing
End Sub
 
M

Michael Bauer

Am Sat, 25 Feb 2006 11:37:19 -0800 schrieb Edward:

Edward, please see the VBA help for a sample of AdvancedSearch.
strF = "urn:schemas:httpmail:body LIKE '<' & * & '>'"

For searching something that is enclosed in the brackets use:

strF = "urn:schemas:httpmail:textdescription LIKE '<%>'"
 
E

Edward

The VBA help isn't too clear on how to access the results of the
AdvancedSearch. Do I need a streamwriter to get the info to a text file, or
can I access the object directly somehow?

Much thanks.
 
M

Michael Bauer

Am Sun, 26 Feb 2006 17:29:02 -0800 schrieb Edward:

Accessing the result is the one thing and very clear: Use the
AdvancedSearchComplete event. If that fires then the search is complete and
it provides you with a Search object which has a Result property to access
all the result items.

What you want to do with that result is another thing and can't be part of
the help file. You can use e.g. the FileSystemObject of the Scripting
Runtime Library to write any data in a text file.
 
E

Edward

Still confused. Any idea why this doesn't work:



Sub SearchUndeliverablesFolder()
'Searches the Inbox
Dim objSch As Search
Const strF As String = "urn:schemas:httpmail:textdescription LIKE '<%>'"
Const strS As String = "undeliverables"
Const strTag As String = "GITem"
Set objSch = Application.AdvancedSearch(Scope:=strS, _
Filter:=strF, Tag:=strTag)
End Sub


Public Sub Application_AdvancedSearchComplete(ByVal objSch As Search)
Dim objRsts As Results

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\UndelResults.txt", True)

MsgBox "The search " & objSch.Tag & "has completed."
Set objRsts = objSch.Results
'Display number in Results collection
MsgBox "There are " & objRsts.Count & "address items"
'Write each member of Results to file
For Each Item In objRsts
a.WriteLine (Item) 'took this out: objRsts.WriteLine Item
Next
a.Close
MsgBox "end of write subroutine", vbOKOnly + vbInformation
End Sub

MsgBox "Finished.", vbOKOnly + vbInformation

Set objNS = Nothing
Set objFolder = Nothing
Set objItem = Nothing
Set objItems = Nothing


End Sub


tHANKS
 
D

davew

Hi Edward,
Did you ever get the code to work for collecting all undeliverable
then writing just the email addresses to a seperate file?

If so have I could really use it as I have a large number o
undeliverables that need listing.

Cheers
Dav
 

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