Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook custom search - at least one invalid argument 1

Status
Not open for further replies.

MakeItSo

Programmer
Oct 21, 2003
3,316
DE
Hello friends,

I am trying to create a custom search function for Outlook.
Background: our managers store project-related mails in singular psts in our file system, together with all other project material.
As they seldom only have one project at a time, they usually have several psts open in Outlook 2003.
Naturally, they sometimes have to search for specific mails and may not know in which project the issue had occured.

==>That is why I try to create a custom Outlook VBA search function, searching across all opened psts.

This is what I have so far:
Code:
Dim pst As MAPIFolder, fol As MAPIFolder
Dim objSch As Search
Dim strF As String
Dim strS As String, i As Integer, j As Integer
Dim ns As Outlook.NameSpace
Dim inb As Object
Const strTag As String = "SubjectSearch"

On Error Resume Next

strF = "urn:schemas:mailheader:subject LIKE '%" & UserForm1.TextBox1.Text & "%'"
'Const strS As String = "Inbox"

Set ns = Outlook.GetNamespace("MAPI")
Set itm = ns.GetDefaultFolder(olFolderInbox)
Set fol = itm.Folders.Add("My global findings")

Debug.Print Outlook.Session.Folders.Count

For Each pst In Outlook.Session.Folders
    j = j + 1
    Set objSch = pst.Application.AdvancedSearch(Scope:=pst.Name, Filter:=strF, SearchSubFolders:=True, Tag:=strTag & j)
    If objSch.Results.Count > 0 Then
        For i = 1 To objSch.Results.Count
            objSch.Results.Item(i).Copy fol.Name
        Next i
    End If
Next pst

As you can see, I create a folder in the inbox into which I copy all found mails. (Loved to simply list links to the mails in a grid, but don't know how to do that yet. Will be amended later)

It does cycle through the psts correctly, but the AdvancedSearch function always errors out with "one or more arguments invalid".

Do any of you know why and what I'm doing wrong?

Thanks a lot!
Andy

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
You may try to replace this:
[tt]Scope:=pst.Name[/tt]
with this:
[tt]Scope:="'" & pst.Name & "'"[/tt]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV!
That was not the error, but it made me think of something else:

It was actually the "Scope" parameter that caused the error. I cannot assign the pst itself as the scope, so I changed my code to:
Code:
[b]boxes = Array("'Inbox'", "'Sent Items'")[/b]
On Error Resume Next

strF = "urn:schemas:mailheader:subject LIKE '%" & UserForm1.TextBox1.Text & "%'"
'Const strS As String = "Inbox"

Set ns = Outlook.GetNamespace("MAPI")
Set itm = ns.GetDefaultFolder(olFolderInbox)
Set fol = itm.Folders.Add("Globale Suchergebnisse")

Debug.Print Outlook.Session.Folders.Count
For Each pst In Outlook.Session.Folders
    Err.Clear
    For j = 0 To 1
        Set objSch = pst.Application.AdvancedSearch([b]Scope:=boxes(j),[/b] Filter:=strF, SearchSubFolders:=True, Tag:=strTag)
        If objSch.Results.Count > 0 Then
            For i = 1 To objSch.Results.Count
                [red]objSch.Results.Item(i).Copy fol.Name[/red]
            Next i
        End If
    Next j
Next pst

So now, I apply the search on both Inbox and sent Items of each PST, and it seems to work!

Alas: the red part doesn't. It seems I can only MOVE mailitems to a different folder but not copy them.
[ponder]

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Got it!!!

it was idiotically simple:
Code:
Dim mi as MailItem
...
...
Set mi = objSch.Results.Item(i).Copy
mi.Move fol
...
:p

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top