Outlook Advanced Search Problems...returns with an Error: Argument

Status
Not open for further replies.
A

amatuercoder151

Hi All,

I am facing a problem with using the advanced search function provided in

the outlook object model.

What i am trying to achieve is simple. I just want to open a pst, and able

to run a search on all the items( calender, mail, appointment...everything),

and then just get the all items count...thats all..

The code i have written is as follows:

Dim app As New Application

Dim pstinfo As [NameSpace]

pstinfo = app.GetNamespace("MAPI")

Dim objfolder As MAPIFolder = Nothing

app.Session.Logon("outlook", "", False, True)

Dim allfolders As Microsoft.Office.Interop.Outlook.Folders =

pstinfo.Folders

Dim i As Integer = 0

Dim ii As Integer = 0

Dim foldernames(10) As String

For Each folder In allfolders

Dim objtempfolder As MAPIFolder = folder

foldernames(i) = objtempfolder.Name

i = i + 1

Next

pstinfo.Session.AddStore(PST NAME HERE)

Dim matchfound As Integer = 0

For Each folder In pstinfo.Folders

matchfound = 0

Dim objtempfolder1 As MAPIFolder = folder

For i = 0 To 10

If objtempfolder1.Name = foldernames(i) Then

matchfound = 1

Exit For

End If

Next

If matchfound = 0 Then

objfolder = objtempfolder1

End If

Next

Dim scope As String = Nothing

scope = "'" & objfolder.folderpath & "'"

Dim search As Search = app.Session.Application.AdvancedSearch(scope,

, True, "")

pstinfo.Session.RemoveStore(objfolder)

app = Nothing

pstinfo = Nothing

objfolder = Nothing

search = Nothing

System.GC.Collect()

Private Sub app_AdvancedSearchComplete(ByVal SearchObject As

Microsoft.Office.Interop.Outlook.Search) Handles app.AdvancedSearchComplete

Dim results As Results

results = SearchObject.Results()

MsgBox("The Total Number of found in the PST Are: " & results.Count)

End Sub

This code works totally fine with almost all the PST's, until i encountered

a PST which had a mailbox named " Archived Folders". Thereby the

objfolder.folderpath variable comes out to be "\\ Archived Folders", and the

advanced search line throws an error saying Error: Argument Exception was

Unhandled:Could not complete the operation. One or more parameter values are

not valid.

For all others its working. Also one thing to mention is that "\\ Archived

Folders" has a starting space character, which i feel might be the problem.

Please if somebody could help me out, as i have tried all things, but

nothing works
 
K

Ken Slovak - [MVP - Outlook]

See if replacing any spaces with "%20" works.

"amatuercoder151" <amatuercoder151> wrote in

message news:3A5C0A37-80A8-40F8-B084-C7103B7029D1@microsoft.com...
> Hi All,
> I am facing a problem with using the advanced search function provided in
> the outlook object model.
> What i am trying to achieve is simple. I just want to open a pst, and able
> to run a search on all the items( calender, mail,
> appointment...everything),
> and then just get the all items count...thats all..
> The code i have written is as follows:

> Dim app As New Application
> Dim pstinfo As [NameSpace]
> pstinfo = app.GetNamespace("MAPI")
> Dim objfolder As MAPIFolder = Nothing
> app.Session.Logon("outlook", "", False, True)
> Dim allfolders As Microsoft.Office.Interop.Outlook.Folders =
> pstinfo.Folders
> Dim i As Integer = 0
> Dim ii As Integer = 0
> Dim foldernames(10) As String
> For Each folder In allfolders
> Dim objtempfolder As MAPIFolder = folder
> foldernames(i) = objtempfolder.Name
> i = i + 1

> Next
> pstinfo.Session.AddStore(PST NAME HERE)

> Dim matchfound As Integer = 0
> For Each folder In pstinfo.Folders
> matchfound = 0
> Dim objtempfolder1 As MAPIFolder = folder
> For i = 0 To 10
> If objtempfolder1.Name = foldernames(i) Then
> matchfound = 1
> Exit For

> End If
> Next
> If matchfound = 0 Then
> objfolder = objtempfolder1
> End If

> Next

> Dim scope As String = Nothing
> scope = "'" & objfolder.folderpath & "'"

> Dim search As Search =
> app.Session.Application.AdvancedSearch(scope,
> , True, "")
> pstinfo.Session.RemoveStore(objfolder)
> app = Nothing
> pstinfo = Nothing
> objfolder = Nothing
> search = Nothing
> System.GC.Collect()

> Private Sub app_AdvancedSearchComplete(ByVal SearchObject As
> Microsoft.Office.Interop.Outlook.Search) Handles
> app.AdvancedSearchComplete
> Dim results As Results
> results = SearchObject.Results()
> MsgBox("The Total Number of found in the PST Are: " &
> results.Count)
> End Sub

> This code works totally fine with almost all the PST's, until i
> encountered
> a PST which had a mailbox named " Archived Folders". Thereby the
> objfolder.folderpath variable comes out to be "\\ Archived Folders", and
> the
> advanced search line throws an error saying Error: Argument Exception was
> Unhandled:Could not complete the operation. One or more parameter values
> are
> not valid.
> For all others its working. Also one thing to mention is that "\\ Archived
> Folders" has a starting space character, which i feel might be the
> problem.
> Please if somebody could help me out, as i have tried all things, but
> nothing works
>
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
mll persistently customise columns in outlook advanced search Using Outlook 3
R Outlook for Mac - Advanced Search and Tracking Sent Emails Using Outlook 0
B Advanced Search in MS Outlook by VBA and SQL Outlook VBA and Custom Forms 2
T Outlook 2007 Indexing Options and Advanced Search options disabled Using Outlook 5
P Outlook Advanced Find Using Outlook 0
N Outlook 2010 crippled Advanced Find, now what? Using Outlook 3
H Outlook 2010 Advanced Find - Menu items missing Using Outlook 1
D Outlook 2010 - Advanced Find - No Status Bar = No Item Count Using Outlook 2
J Advanced outlook trick: help needed! Outlook VBA and Custom Forms 3
N Can't create NEW GROUP and add/remove a member from existing Group in Outlook Using Outlook 1
P Outlook tasks keeps changing (updating) dates that I type Using Outlook 2
M I cant send emails via Outlook in my W10 PC. Using Outlook 3
A Outlook Reminders not populating for certain events Using Outlook 2
O Moving "tasks" to inbox in Outlook 2016 Using Outlook 1
S HTML Code Embedded in String Within Open Outlook Email Preventing Replace(Application.ActiveInspector.CurrentItem.HTMLBody From Working Outlook VBA and Custom Forms 4
e_a_g_l_e_p_i Can emails from Gmail be deleted when they are downloaded to Outlook 2010 Using Outlook 1
cbufacchi Outlook 365 Populate custom Outlook Appoint form Outlook VBA and Custom Forms 2
Z Remove GMAIL IMAP account from Outlook 2016 Using Outlook 2
D Wrong email address in Outlook 2003 "From" tab in new outgoing emails Using Outlook 4
B Outlook 365 Populate Outlook Task UDFs from a UDF text string Outlook VBA and Custom Forms 2
witzker Outlook 2019 Pls. add a Prefix for OUTLOOK 2019 here Using Outlook 1
H Outlook 2019 intermittent search results Using Outlook 0
hstein POP3 latest Outlook Using Outlook 0
V Outlook 2003 and Windows 11 Using Outlook 4
J How to import many msg into different public folders in Outlook Outlook VBA and Custom Forms 7
I Outlook for Mac 2019 using on desktop and laptop IMAP on both need help with folders Using Outlook 1
A Reminder duplication Outlook and Outlook.com Using Outlook.com accounts in Outlook 5
F Moving Outlook to new PC Using Outlook 0
Ken Pascoe Outlook Categories Quick List Using Outlook 0
M All fonts in Outlook emails display with exaggerated character spacing Using Outlook 3
talla Can't open Outlook Item. Using Outlook 0
C Can't Locate an Unread Message in my Outlook view pane Using Outlook 0
C Outlook 2007 Removing then adding account restores junk email processing Using Outlook 0
P Importing other e-mail accounts into Outlook Using Outlook 1
J Read Outlook Form fields Outlook VBA and Custom Forms 3
B Inconsistent handling of message read/unread status by Outlook Using Outlook 3
R Rogue Outlook Rule ? Using Outlook 2
S vba outlook search string with special characters Outlook VBA and Custom Forms 1
F Wishlist Outlook suddenly began synchronizing deleted items every time I delete a single email. Using Outlook 2
U Outlook 2019 VBA run-time error 424 Outlook VBA and Custom Forms 2
K Outlook 2019 Randomly Disconnecting from Gmail Servers Using Outlook 8
P Outlook calendar and contacts sync problem-outlook disconnects Using Outlook.com accounts in Outlook 2
HarvMan Toggle between calendar and email in Outlook 365 Using Outlook 12
V Outlook error 500 Using Outlook 2
F Email being marked as Spam by Gmail and not being visible in Outlook Using Outlook 5
S Mac Outlook 365 Questions Using Outlook 1
M Outlook calendar is missing Using Outlook 2
G Save and Rename Outlook Email Attachments Outlook VBA and Custom Forms 0
G Trigger script without restaring outlook Outlook VBA and Custom Forms 7
T Have you written an articles about Outlook? Using Outlook 3

Similar threads

Top