To Sue Mosher - Count of returned results is changing?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dear Sue,

I'm new to Outlook VBA and have been beating my head on a routine that
searches large amounts of messages which I then use to copy/move the results
to another store.

Basically, I kick off 51 AdvancedSearch methods, and wait for them all to
complete. All results go into an array of search objects. When I go to access
each search object, it's Results.Count increments by one in the middle of
copying the messages. This does not make sense, and the program crashes on
the last item since I've clearly gone beyond the # of Result.Item(s).

Here's the code snippet:

' if there are results, copy them

If arrSearch(j_CopyData).Results.Count > 0 Then
Call UpdateProgress(vbTab & vbTab & "Copy " &
arrSearch(j_CopyData).Results.Count & _
" messages to " &
objNS.Folders.Item(strStoreName).Folders.Item(strSearchedStoreName).FolderPath)
k_CopyData = 1 ' Keep count
For Each objResult In arrSearch(j_CopyData).Results
Call UpdateProgress(vbTab & vbTab & vbTab & "Store: " & j_CopyData & "
Item No: " & k_CopyData & " of " & arrSearch(j_CopyData).Results.Count & "
Subj: " & objResult.Subject)
Set objMessage = objResult.Copy
objMessage =
objMessage.Move(objNS.Folders.Item.strStoreName).Folders.Item(strSearchedStoreName))
k_CopyData = k_CopyData + 1
DoEvents
Next
End If

Output:
---------

(Here, I get "Method 'Copy' of 'MailItem' Failed" RunTime Error Msg)

11:51:16 Store: 1 Item No: 167 of 167 Subj: FW: INSURANCE RUN-OFFS No.40
11:51:16 Store: 1 Item No: 166 of 167 Subj: FW: Reinsurance Committee
meeting - France Telecom/Hannover Re
11:51:16 Store: 1 Item No: 165 of 167 Subj: FW: Reinsurance Committee
meeting - France Telecom/Hannover Re
11:51:16 Store: 1 Item No: 164 of 167 Subj: RE: Reinsurance Committee
meeting - France Telecom/Hannover Re
11:51:15 Store: 1 Item No: 163 of 167 Subj: RE: Hannover
..
..
..
11:50:50 Store: 1 Item No: 57 of 167 Subj: Update: Renewal of Hannover
Re Top Layer
11:50:50 Store: 1 Item No: 56 of 167 Subj: Germany Trip
11:50:50 Store: 1 Item No: 55 of 167 Subj: hannover re renewal
11:50:50 Store: 1 Item No: 54 of 167 Subj: RE: Germany Trip
11:50:49 Store: 1 Item No: 53 of 166 Subj: FW: Update: Renewal of
Hannover Re Top Layer
11:50:49 Store: 1 Item No: 52 of 166 Subj: RE: Trenwick LPT Proposal
11:50:49 Store: 1 Item No: 51 of 166 Subj: FW: Trenwick LPT Proposal
..
..
..
..
11:50:39 Store: 1 Item No: 10 of 166 Subj: RE: Hannover
11:50:39 Store: 1 Item No: 9 of 166 Subj: NEWS - 6/20/2003
11:50:39 Store: 1 Item No: 8 of 166 Subj: Email 2 of 3 Guidance Notes on
Hannover
11:50:38 Store: 1 Item No: 7 of 166 Subj: EIN24, 23 June
11:50:38 Store: 1 Item No: 6 of 166 Subj: Hannover Re Top Layer Legal
review and Signing of Slip
11:50:38 Store: 1 Item No: 5 of 166 Subj: EIN24, 11 July
11:50:38 Store: 1 Item No: 4 of 166 Subj: Accra model
11:50:38 Store: 1 Item No: 3 of 166 Subj: FW: Accra model
11:50:37 Store: 1 Item No: 2 of 166 Subj: RE: Accra model
11:50:37 Store: 1 Item No: 1 of 166 Subj: RE: Accra model
11:50:37 Copy 166 messages to \\P-1\Personal Folders
11:50:37 Add folder: \\P-1\Personal Folders
 
Please Note: Your book is great, and very useful. I just can't understand
why a count of returned messages changes AFTER a search is complete....
 
Try using a separate object variable for the results and a For Each loop to
process the results:

Set myResults = arrSearch(j_CopyData).Results
If myResults.Count > 0 Then
' etc.
For Each myItem in myResults
' do the copying here.
Next
End If

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
That did not work. However, adding an if statement that checked my counter
vs. objNS.Folders.Count made everything stay fixed and not run over the end
of the Search.Results collection.

Who knows.

Thanks again and Happy New Year.

Sue Mosher said:
Try using a separate object variable for the results and a For Each loop to
process the results:

Set myResults = arrSearch(j_CopyData).Results
If myResults.Count > 0 Then
' etc.
For Each myItem in myResults
' do the copying here.
Next
End If

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Back
Top